/**
 * Copyright (c) 2014-present, Facebook, Inc. All rights reserved.
 *
 * This source code is licensed under the BSD-style license found in the
 * LICENSE file in the root directory of this source tree. An additional grant
 * of patent rights can be found in the PATENTS file in the same directory.
 *
 * @flow
 */

import type {
  Colors,
  Indent,
  PluginOptions,
  Print,
  Plugin,
} from 'types/PrettyFormat';

import ansiRegex from 'ansi-regex';
import style from 'ansi-styles';

const toHumanReadableAnsi = text => {
  return text.replace(ansiRegex(), (match, offset, string) => {
    switch (match) {
      case style.red.close:
      case style.green.close:
      case style.reset.open:
      case style.reset.close:
        return '</>';
      case style.red.open:
        return '<red>';
      case style.green.open:
        return '<green>';
      case style.dim.open:
        return '<dim>';
      case style.bold.open:
        return '<bold>';
      default:
        return '';
    }
  });
};

export const test = (value: any) =>
  typeof value === 'string' && value.match(ansiRegex());

export const print = (
  val: any,
  print: Print,
  indent: Indent,
  opts: PluginOptions,
  colors: Colors,
) => print(toHumanReadableAnsi(val));

export default ({print, test}: Plugin);

@@ -329,65 +329,40 @@
 pe %7B
-%0A  Colors,%0A  Indent,%0A  PluginOptions,%0A  Print,%0A  
+Config, Printer, New
 Plugin,
-%0A
+ Refs
 %7D fr
@@ -1003,18 +1003,16 @@
 t = (val
-ue
 : any) =
@@ -1025,18 +1025,16 @@
 peof val
-ue
  === 'st
@@ -1049,10 +1049,8 @@
  val
-ue
 .mat
@@ -1080,21 +1080,25 @@
 t const 
-print
+serialize
  = (%0A  v
@@ -1105,86 +1105,106 @@
 al: 
-any,%0A  print: Print,%0A  indent: Indent,%0A  opts: PluginOptions,%0A  colors: Colors
+string,%0A  config: Config,%0A  indentation: string,%0A  depth: number,%0A  refs: Refs,%0A  printer: Printer
 ,%0A) 
@@ -1211,16 +1211,18 @@
 =%3E print
+er
 (toHuman
@@ -1238,16 +1238,50 @@
 nsi(val)
+, config, indentation, depth, refs
 );%0A%0Aexpo
@@ -1293,21 +1293,25 @@
 fault (%7B
-print
+serialize
 , test%7D:
@@ -1311,16 +1311,19 @@
  test%7D: 
+New
 Plugin);

Replace print with serialize in ConvertAnsi plugin (#4225)