"use strict";
var __defProp = Object.defineProperty;
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
var __getOwnPropNames = Object.getOwnPropertyNames;
var __hasOwnProp = Object.prototype.hasOwnProperty;
var __export = (target, all) => {
  for (var name in all)
    __defProp(target, name, { get: all[name], enumerable: true });
};
var __copyProps = (to, from, except, desc) => {
  if (from && typeof from === "object" || typeof from === "function") {
    for (let key of __getOwnPropNames(from))
      if (!__hasOwnProp.call(to, key) && key !== except)
        __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
  }
  return to;
};
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);

// src/utils/index.ts
var utils_exports = {};
__export(utils_exports, {
  convertTableToCSV: () => convertTableToCSV,
  escapeCsvField: () => escapeCsvField,
  markdownToPlainText: () => markdownToPlainText,
  mergeRefs: () => mergeRefs,
  parseMarkdownTable: () => parseMarkdownTable
});
module.exports = __toCommonJS(utils_exports);

// src/utils/markdown-to-plain-text.ts
var PLACEHOLDER_PREFIX = "\u200B\u200BIC";
var PLACEHOLDER_SUFFIX = "\u200B\u200B";
var PLACEHOLDER_REGEX = new RegExp(`${PLACEHOLDER_PREFIX}(\\d+)${PLACEHOLDER_SUFFIX}`, "g");
function stripInlineMarks(text) {
  return text.replace(/\*{3}(.+?)\*{3}/g, "$1").replace(/\*{2}(.+?)\*{2}/g, "$1").replace(/_{2}(.+?)_{2}/g, "$1").replace(/\*(.+?)\*/g, "$1").replace(/_(.+?)_/g, "$1").replace(/~~(.+?)~~/g, "$1");
}
function markdownToPlainText(markdown) {
  if (!markdown) return "";
  const inlineCodeSnippets = [];
  let result = markdown.replace(/^<!--[\s\S]*?--!?>\n?/gm, "").replace(/```[\s\S]*?```/g, "").replace(/`([^`]+)`/g, (_match, code) => {
    inlineCodeSnippets.push(code);
    return `${PLACEHOLDER_PREFIX}${inlineCodeSnippets.length - 1}${PLACEHOLDER_SUFFIX}`;
  }).replace(/^#{1,6}\s+/gm, "").replace(/^(?:---|\*\*\*|___)\s*$/gm, "");
  result = stripInlineMarks(result).replace(/!\[([^\]]*)\]\([^)]+\)/g, "$1").replace(/\[([^\]]+)\]\([^)]+\)/g, "$1").replace(/^>\s?/gm, "").replace(/^\|[-:\s|]+\|\s*$/gm, "").replace(/^\|\s?/gm, "").replace(/\s?\|$/gm, "").replace(/\n{3,}/g, "\n\n").trim();
  result = result.replace(PLACEHOLDER_REGEX, (_match, index) => inlineCodeSnippets[Number(index)]);
  return result;
}

// src/utils/merge-refs.ts
function mergeRefs(...refs) {
  return (node) => {
    for (const ref of refs) {
      if (typeof ref === "function") {
        ref(node);
      } else if (ref) {
        ref.current = node;
      }
    }
  };
}

// src/utils/parse-markdown-table.ts
function parseCells(line) {
  const trimmed = line.trim();
  const stripped = trimmed.startsWith("|") ? trimmed.slice(1) : trimmed;
  const withoutTrailing = stripped.endsWith("|") ? stripped.slice(0, -1) : stripped;
  return withoutTrailing.split("|").map((cell) => cell.trim());
}
function isSeparatorRow(line) {
  const cells = parseCells(line);
  return cells.length > 0 && cells.every((cell) => /^:?-{1,}:?$/.test(cell));
}
function isTableRow(line) {
  return line.includes("|");
}
var TABLE_TITLE_PREFIX = "<!-- table-title:";
var TABLE_TITLE_SUFFIX = "-->";
function extractTableTitle(line) {
  const trimmed = line.trim();
  if (!trimmed.startsWith(TABLE_TITLE_PREFIX) || !trimmed.endsWith(TABLE_TITLE_SUFFIX)) {
    return void 0;
  }
  return trimmed.slice(TABLE_TITLE_PREFIX.length, -TABLE_TITLE_SUFFIX.length).trim() || void 0;
}
function parseMarkdownTable(markdown, tableIndex = 0) {
  const lines = markdown.split("\n");
  let tableCount = 0;
  for (let i = 0; i < lines.length - 1; i++) {
    const headerLine = lines[i];
    const separatorLine = lines[i + 1];
    if (!isTableRow(headerLine) || !isSeparatorRow(separatorLine)) {
      continue;
    }
    if (tableCount === tableIndex) {
      const columns = parseCells(headerLine).map(stripInlineMarks);
      const rows = [];
      const title = i > 0 ? extractTableTitle(lines[i - 1]) : void 0;
      for (let j = i + 2; j < lines.length; j++) {
        if (!isTableRow(lines[j]) || isSeparatorRow(lines[j])) {
          break;
        }
        const trimmedLine = lines[j].trim();
        if (trimmedLine === "") {
          break;
        }
        rows.push(parseCells(lines[j]).map(stripInlineMarks));
      }
      return { columns, rows, ...title ? { title } : {} };
    }
    tableCount++;
  }
  return null;
}

// src/utils/escape-csv-field.ts
var FORMULA_TRIGGERS = /* @__PURE__ */ new Set(["=", "+", "@", "	", "\r"]);
function escapeCsvField(field) {
  const needsNeutralise = field.length > 0 && FORMULA_TRIGGERS.has(field[0]);
  const safeField = needsNeutralise ? `	${field}` : field;
  if (needsNeutralise || safeField.includes(",") || safeField.includes('"') || safeField.includes("\n") || safeField.includes("\r")) {
    return `"${safeField.replace(/"/g, '""')}"`;
  }
  return safeField;
}

// src/utils/convert-table-to-csv.ts
function convertTableToCSV(table) {
  const headerRow = table.columns.map(escapeCsvField).join(",");
  const dataRows = table.rows.map((row) => row.map(escapeCsvField).join(","));
  return [headerRow, ...dataRows].join("\r\n");
}
// Annotate the CommonJS export names for ESM import in node:
0 && (module.exports = {
  convertTableToCSV,
  escapeCsvField,
  markdownToPlainText,
  mergeRefs,
  parseMarkdownTable
});
//# sourceMappingURL=utils.cjs.map