"use strict";

Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.MarkDown = void 0;
var _react = _interopRequireDefault(require("react"));
var _reactNative = require("react-native");
var _jsxRuntime = require("react/jsx-runtime");
function _interopRequireDefault(e) { return e && e.__esModule ? e : { default: e }; }
const MarkDown = text => {
  if (typeof text !== 'string') {
    return [text];
  }
  let key = 0;
  const elements = [];
  const lines = text.split('\n');
  let inCodeBlock = false;
  let codeLanguage = '';
  const codeBuffer = [];
  const parseInline = input => {
    const output = [];
    const regex = /(\*\*\*[^*]+?\*\*\*|\*\*[^*]+?\*\*|\*[^*]+?\*|~~[^~]+?~~|`[^`]+?`|https:\/\/[\w.-]+(?:\.[\w.-]+)+[\w\-._~:/?#[\]@!$&'()*+,;=]+)/g;
    let lastIndex = 0;
    let match;
    while ((match = regex.exec(input)) !== null) {
      if (match.index > lastIndex) {
        output.push(input.slice(lastIndex, match.index));
      }
      const token = match[0];
      if (/^\*\*\*.*\*\*\*$/.test(token)) {
        output.push(/*#__PURE__*/(0, _jsxRuntime.jsx)(_reactNative.Text, {
          style: styles.bold,
          children: /*#__PURE__*/(0, _jsxRuntime.jsx)(_reactNative.Text, {
            style: styles.italic,
            children: token.slice(3, -3)
          })
        }, `b-${key++}`));
      } else if (/^\*\*.*\*\*$/.test(token)) {
        output.push(/*#__PURE__*/(0, _jsxRuntime.jsx)(_reactNative.Text, {
          style: styles.bold,
          children: token.slice(2, -2)
        }, `b-${key++}`));
      } else if (/^\*.*\*$/.test(token)) {
        output.push(/*#__PURE__*/(0, _jsxRuntime.jsx)(_reactNative.Text, {
          style: styles.italic,
          children: token.slice(1, -1)
        }, `i-${key++}`));
      } else if (/^~~.*~~$/.test(token)) {
        output.push(/*#__PURE__*/(0, _jsxRuntime.jsx)(_reactNative.Text, {
          style: styles.strike,
          children: token.slice(2, -2)
        }, `s-${key++}`));
      } else if (/^`.*`$/.test(token)) {
        output.push(/*#__PURE__*/(0, _jsxRuntime.jsx)(_reactNative.Text, {
          style: styles.codeInline,
          children: token.slice(1, -1).trim()
        }, `code-${key++}`));
      } else if (/^https:\/\//.test(token)) {
        output.push(/*#__PURE__*/(0, _jsxRuntime.jsx)(_reactNative.Text, {
          children: /*#__PURE__*/(0, _jsxRuntime.jsx)(_reactNative.Text, {
            onPress: () => _reactNative.Linking.openURL(token),
            style: styles.link,
            children: token
          })
        }, `link-${key++}`));
      }
      lastIndex = match.index + token.length;
    }
    if (lastIndex < input.length) {
      output.push(input.slice(lastIndex));
    }
    return output;
  };
  const parseList = startIndex => {
    const items = [];
    const line = lines[startIndex].trim();
    const match = line.match(/^(\d+)\./);
    const isOrdered = !!match;
    const start = match ? parseInt(match[1], 10) : 1;
    let i = startIndex;
    while (i < lines.length) {
      const currentLine = lines[i];
      if (/^(\d+\.\s+|\-\s+)/.test(currentLine)) {
        const itemText = currentLine.replace(/^(\d+\.\s+|\-\s+)/, '');
        items.push(/*#__PURE__*/(0, _jsxRuntime.jsxs)(_reactNative.View, {
          style: styles.listItem,
          children: [/*#__PURE__*/(0, _jsxRuntime.jsx)(_reactNative.Text, {
            style: styles.listMarker,
            children: isOrdered ? `${start + items.length - 1}.` : '•'
          }), /*#__PURE__*/(0, _jsxRuntime.jsx)(_reactNative.View, {
            style: styles.listItemContent,
            children: parseInline(itemText).map((el, idx) => /*#__PURE__*/(0, _jsxRuntime.jsx)(_react.default.Fragment, {
              children: el
            }, idx))
          })]
        }, `li-${key++}`));
      } else if (currentLine.trim() === '') {
        break;
      } else {
        break;
      }
      i++;
    }
    const ListTag = isOrdered ? 'ol' : 'ul';
    return {
      list: /*#__PURE__*/(0, _jsxRuntime.jsx)(_reactNative.View, {
        style: styles.list,
        children: items
      }, `list-${key++}`),
      newIndex: i - 1
    };
  };
  for (let idx = 0; idx < lines.length; idx++) {
    const line = lines[idx];
    if (!inCodeBlock && line.trim().startsWith('```')) {
      const match = line.trim().match(/^```(?:\s*(\w+))?/);
      inCodeBlock = true;
      codeLanguage = match?.[1] || '';
      continue;
    }
    if (inCodeBlock && line.trim() === '```') {
      inCodeBlock = false;
      elements.push(/*#__PURE__*/(0, _jsxRuntime.jsx)(_reactNative.View, {
        style: styles.codeBlock,
        children: /*#__PURE__*/(0, _jsxRuntime.jsx)(_reactNative.Text, {
          style: styles.codeBlockText,
          children: codeBuffer.join('\n')
        })
      }, `pre-${key++}`));
      codeBuffer.length = 0;
      codeLanguage = '';
      continue;
    }
    if (inCodeBlock) {
      codeBuffer.push(line);
      continue;
    }
    if (/^#{1,6}\s/.test(line)) {
      const level = line.match(/^#+/)[0].length;
      const content = line.replace(/^#{1,6}\s/, '');
      const headingStyle = styles[`h${level}`] || styles.h1;
      elements.push(/*#__PURE__*/(0, _jsxRuntime.jsx)(_reactNative.Text, {
        style: headingStyle,
        children: parseInline(content).map((el, idx) => /*#__PURE__*/(0, _jsxRuntime.jsx)(_react.default.Fragment, {
          children: el
        }, idx))
      }, `h-${key++}`));
      continue;
    }
    if (line.startsWith('>')) {
      elements.push(/*#__PURE__*/(0, _jsxRuntime.jsx)(_reactNative.View, {
        style: styles.quote,
        children: parseInline(line.replace(/^>\s*/, '')).map((el, idx) => /*#__PURE__*/(0, _jsxRuntime.jsx)(_react.default.Fragment, {
          children: el
        }, idx))
      }, `quote-${key++}`));
      continue;
    }
    if (/^(\-|\d+\.)\s+/.test(line)) {
      const {
        list,
        newIndex
      } = parseList(idx);
      elements.push(list);
      idx = newIndex;
      continue;
    }
    if (line.trim() === '') {
      elements.push(/*#__PURE__*/(0, _jsxRuntime.jsx)(_reactNative.View, {
        style: styles.break
      }, `br-${key++}`));
    } else {
      elements.push(/*#__PURE__*/(0, _jsxRuntime.jsx)(_reactNative.Text, {
        style: styles.paragraph,
        children: parseInline(line).map((el, idx) => /*#__PURE__*/(0, _jsxRuntime.jsx)(_react.default.Fragment, {
          children: el
        }, idx))
      }, `p-${key++}`));
    }
  }
  return elements;
};
exports.MarkDown = MarkDown;
const styles = _reactNative.StyleSheet.create({
  bold: {
    fontWeight: 'bold'
  },
  italic: {
    fontStyle: 'italic'
  },
  strike: {
    textDecorationLine: 'line-through'
  },
  codeInline: {
    backgroundColor: '#eee',
    paddingHorizontal: 4,
    paddingVertical: 1,
    borderRadius: 4,
    fontFamily: 'monospace',
    fontSize: 14
  },
  link: {
    color: 'blue',
    textDecorationLine: 'underline'
  },
  codeBlock: {
    backgroundColor: '#f0f0f0',
    padding: 10,
    borderRadius: 8,
    marginVertical: 8
  },
  codeBlockText: {
    fontFamily: 'monospace',
    fontSize: 14
  },
  h1: {
    fontSize: 32,
    fontWeight: 'bold',
    marginTop: 24,
    marginBottom: 16
  },
  h2: {
    fontSize: 24,
    fontWeight: 'bold',
    marginTop: 20,
    marginBottom: 14
  },
  h3: {
    fontSize: 20,
    fontWeight: 'bold',
    marginTop: 16,
    marginBottom: 12
  },
  h4: {
    fontSize: 18,
    fontWeight: 'bold',
    marginTop: 14,
    marginBottom: 10
  },
  h5: {
    fontSize: 16,
    fontWeight: 'bold',
    marginTop: 12,
    marginBottom: 8
  },
  h6: {
    fontSize: 14,
    fontWeight: 'bold',
    marginTop: 12,
    marginBottom: 8
  },
  quote: {
    borderLeftWidth: 3,
    borderLeftColor: '#ccc',
    paddingLeft: 10,
    marginVertical: 4
  },
  list: {
    marginVertical: 8,
    paddingLeft: 20
  },
  listItem: {
    flexDirection: 'row',
    marginBottom: 4
  },
  listMarker: {
    marginRight: 8,
    minWidth: 20
  },
  listItemContent: {
    flex: 1
  },
  paragraph: {
    marginVertical: 4
  },
  break: {
    height: 8
  }
});
//# sourceMappingURL=markdownParser.js.map