1"use strict";
2var __importDefault = (this && this.__importDefault) || function (mod) {
3    return (mod && mod.__esModule) ? mod : { "default": mod };
4};
5Object.defineProperty(exports, "__esModule", { value: true });
6exports.Ansi = void 0;
7/**
8 * Copyright (c) 650 Industries.
9 * Copyright (c) Meta Platforms, Inc. and affiliates.
10 *
11 * This source code is licensed under the MIT license found in the
12 * LICENSE file in the root directory of this source tree.
13 */
14const anser_1 = __importDefault(require("anser"));
15const react_1 = __importDefault(require("react"));
16const react_native_1 = require("react-native");
17// Afterglow theme from https://iterm2colorschemes.com/
18const COLORS = {
19    'ansi-black': 'rgb(27, 27, 27)',
20    'ansi-red': 'rgb(187, 86, 83)',
21    'ansi-green': 'rgb(144, 157, 98)',
22    'ansi-yellow': 'rgb(234, 193, 121)',
23    'ansi-blue': 'rgb(125, 169, 199)',
24    'ansi-magenta': 'rgb(176, 101, 151)',
25    'ansi-cyan': 'rgb(140, 220, 216)',
26    // Instead of white, use the default color provided to the component
27    // 'ansi-white': 'rgb(216, 216, 216)',
28    'ansi-bright-black': 'rgb(98, 98, 98)',
29    'ansi-bright-red': 'rgb(187, 86, 83)',
30    'ansi-bright-green': 'rgb(144, 157, 98)',
31    'ansi-bright-yellow': 'rgb(234, 193, 121)',
32    'ansi-bright-blue': 'rgb(125, 169, 199)',
33    'ansi-bright-magenta': 'rgb(176, 101, 151)',
34    'ansi-bright-cyan': 'rgb(140, 220, 216)',
35    'ansi-bright-white': 'rgb(247, 247, 247)',
36};
37function Ansi({ text, style }) {
38    let commonWhitespaceLength = Infinity;
39    const parsedLines = text.split(/\n/).map((line) => anser_1.default.ansiToJson(line, {
40        json: true,
41        remove_empty: true,
42        use_classes: true,
43    }));
44    parsedLines.map((lines) => {
45        // The third item on each line includes the whitespace of the source code.
46        // We are looking for the least amount of common whitespace to trim all lines.
47        // Example: Array [" ", " 96 |", "     text", ...]
48        const match = lines[2] && lines[2]?.content?.match(/^ +/);
49        const whitespaceLength = (match && match[0]?.length) || 0;
50        if (whitespaceLength < commonWhitespaceLength) {
51            commonWhitespaceLength = whitespaceLength;
52        }
53    });
54    const getText = (content, key) => {
55        if (key === 1) {
56            // Remove the vertical bar after line numbers
57            return content.replace(/\| $/, ' ');
58        }
59        else if (key === 2 && commonWhitespaceLength < Infinity) {
60            // Remove common whitespace at the beginning of the line
61            return content.substr(commonWhitespaceLength);
62        }
63        else {
64            return content;
65        }
66    };
67    return (react_1.default.createElement(react_native_1.View, null, parsedLines.map((items, i) => (react_1.default.createElement(react_native_1.View, { style: styles.line, key: i }, items.map((bundle, key) => {
68        const textStyle = bundle.fg && COLORS[bundle.fg]
69            ? {
70                backgroundColor: bundle.bg && COLORS[bundle.bg],
71                color: bundle.fg && COLORS[bundle.fg],
72            }
73            : {
74                backgroundColor: bundle.bg && COLORS[bundle.bg],
75            };
76        return (react_1.default.createElement(react_native_1.Text, { style: [style, textStyle], key: key }, getText(bundle.content, key)));
77    }))))));
78}
79exports.Ansi = Ansi;
80const styles = react_native_1.StyleSheet.create({
81    line: {
82        flexDirection: 'row',
83    },
84});
85//# sourceMappingURL=AnsiHighlight.js.map