'use strict';

// src/platform/token-optimizer/truncate.ts
var truncateText = (text, maxLength) => {
  const charCount = [...text].length;
  if (charCount <= maxLength) {
    return text;
  }
  if (maxLength < 3) {
    return "...";
  }
  return [...text].slice(0, maxLength - 3).join("") + "...";
};
var truncateLines = (text, options) => {
  const { maxCharsPerLine, headLines, tailLines, maxLines } = options;
  let lines = text.split("\n");
  if (headLines !== void 0 && tailLines !== void 0 && lines.length > headLines + tailLines) {
    const head = lines.slice(0, headLines);
    const tail = lines.slice(-tailLines);
    const omittedCount = lines.length - headLines - tailLines;
    lines = [...head, `... (${omittedCount}\uC904 \uC0DD\uB7B5)`, ...tail];
  } else if (headLines !== void 0 && lines.length > headLines) {
    const omittedCount = lines.length - headLines;
    lines = [...lines.slice(0, headLines), `... (${omittedCount}\uC904 \uC0DD\uB7B5)`];
  } else if (tailLines !== void 0 && lines.length > tailLines) {
    const omittedCount = lines.length - tailLines;
    lines = [`... (${omittedCount}\uC904 \uC0DD\uB7B5)`, ...lines.slice(-tailLines)];
  }
  if (maxCharsPerLine !== void 0) {
    lines = lines.map((line) => truncateText(line, maxCharsPerLine));
  }
  if (maxLines !== void 0 && lines.length > maxLines) {
    if (maxLines <= 0) {
      return "";
    }
    const preservedLineCount = Math.max(maxLines - 1, 0);
    const omittedCount = lines.length - preservedLineCount;
    lines = preservedLineCount === 0 ? [`... (${omittedCount}\uC904 \uC0DD\uB7B5)`] : [...lines.slice(0, preservedLineCount), `... (${omittedCount}\uC904 \uC0DD\uB7B5)`];
  }
  return lines.join("\n");
};

exports.truncateLines = truncateLines;
exports.truncateText = truncateText;
