function _array_like_to_array(arr, len) {
    if (len == null || len > arr.length) len = arr.length;
    for(var i = 0, arr2 = new Array(len); i < len; i++)arr2[i] = arr[i];
    return arr2;
}
function _array_without_holes(arr) {
    if (Array.isArray(arr)) return _array_like_to_array(arr);
}
function _iterable_to_array(iter) {
    if (typeof Symbol !== "undefined" && iter[Symbol.iterator] != null || iter["@@iterator"] != null) return Array.from(iter);
}
function _non_iterable_spread() {
    throw new TypeError("Invalid attempt to spread non-iterable instance.\\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method.");
}
function _to_consumable_array(arr) {
    return _array_without_holes(arr) || _iterable_to_array(arr) || _unsupported_iterable_to_array(arr) || _non_iterable_spread();
}
function _unsupported_iterable_to_array(o, minLen) {
    if (!o) return;
    if (typeof o === "string") return _array_like_to_array(o, minLen);
    var n = Object.prototype.toString.call(o).slice(8, -1);
    if (n === "Object" && o.constructor) n = o.constructor.name;
    if (n === "Map" || n === "Set") return Array.from(n);
    if (n === "Arguments" || /^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(n)) return _array_like_to_array(o, minLen);
}
/**
 * @fileoverview 代码编辑器插件主文件
 * 提供基于 Ace Editor 的代码编辑功能，支持多种编程语言语法高亮
 * @author Code Plugin Team
 */ import React from "react";
import { BaseMarkdownEditor } from "../../MarkdownEditor";
import { useEditorStore } from "../../MarkdownEditor/editor/store";
import { CodeRenderer } from "./components";
export function csvToMarkdownTable(csv) {
    if (!csv) return '';
    // 处理不同换行符
    var lines = csv.replace(/\r\n/g, '\n').replace(/\r/g, '\n').split('\n');
    var rows = [];
    var _iteratorNormalCompletion = true, _didIteratorError = false, _iteratorError = undefined;
    try {
        for(var _iterator = lines[Symbol.iterator](), _step; !(_iteratorNormalCompletion = (_step = _iterator.next()).done); _iteratorNormalCompletion = true){
            var line = _step.value;
            if (line.trim() === '') continue;
            var cells = [];
            var cur = '';
            var inQuotes = false;
            for(var i = 0; i < line.length; i++){
                var ch = line[i];
                if (ch === '"') {
                    // 双引号转义或开/关引号
                    if (inQuotes && i + 1 < line.length && line[i + 1] === '"') {
                        cur += '"';
                        i++; // 跳过下一个引号
                    } else {
                        inQuotes = !inQuotes;
                    }
                } else if (ch === ',' && !inQuotes) {
                    cells.push(cur);
                    cur = '';
                } else {
                    cur += ch;
                }
            }
            cells.push(cur);
            rows.push(cells.map(function(c) {
                return c.trim();
            }));
        }
    } catch (err) {
        _didIteratorError = true;
        _iteratorError = err;
    } finally{
        try {
            if (!_iteratorNormalCompletion && _iterator.return != null) {
                _iterator.return();
            }
        } finally{
            if (_didIteratorError) {
                throw _iteratorError;
            }
        }
    }
    if (rows.length === 0) return '';
    // 列数取最大
    var colCount = rows.reduce(function(max, r) {
        return Math.max(max, r.length);
    }, 0);
    var _iteratorNormalCompletion1 = true, _didIteratorError1 = false, _iteratorError1 = undefined;
    try {
        // 规范每行列数
        for(var _iterator1 = rows[Symbol.iterator](), _step1; !(_iteratorNormalCompletion1 = (_step1 = _iterator1.next()).done); _iteratorNormalCompletion1 = true){
            var r = _step1.value;
            while(r.length < colCount)r.push('');
        }
    } catch (err) {
        _didIteratorError1 = true;
        _iteratorError1 = err;
    } finally{
        try {
            if (!_iteratorNormalCompletion1 && _iterator1.return != null) {
                _iterator1.return();
            }
        } finally{
            if (_didIteratorError1) {
                throw _iteratorError1;
            }
        }
    }
    // 第一行为表头
    var header = rows[0];
    var body = rows.slice(1);
    var escapeCell = function escapeCell(s) {
        return s.replace(/\|/g, '\\|');
    };
    var headerLine = "| ".concat(header.map(escapeCell).join(' | '), " |");
    var sepLine = "|:".concat(new Array(colCount).fill('---').join(' | '), " |");
    var bodyLines = body.map(function(r) {
        return "| ".concat(r.map(escapeCell).join(' | '), " |");
    });
    return [
        headerLine,
        sepLine
    ].concat(_to_consumable_array(bodyLines)).join('\n');
}
/**
 * 代码编辑器组件
 * 已重构为更小的组件，提升可维护性
 */ export function CodeElement(props) {
    var readonly = useEditorStore().readonly;
    if (!props.element) {
        return null;
    }
    // readonly 模式下，空代码块不展示
    if (readonly && !props.element.value) {
        return null;
    }
    if (readonly && props.element.language === 'csv') {
        var _props_element;
        return /*#__PURE__*/ React.createElement(BaseMarkdownEditor, {
            contentStyle: {
                padding: 0,
                paddingTop: 32
            },
            style: {
                padding: 0
            },
            initValue: csvToMarkdownTable((_props_element = props.element) === null || _props_element === void 0 ? void 0 : _props_element.value),
            readonly: true
        });
    }
    return /*#__PURE__*/ React.createElement(CodeRenderer, props);
}
