/**
 * 编辑模式模块
 *
 * 提供统一的代码编辑接口，支持多种编辑模式：
 * - whole: 整文件替换
 * - search-replace: 搜索替换（支持多块）
 * - diff: 统一 diff 格式
 */

// 类型导出
export type {
  EditMode,
  Edit,
  WholeFileEdit,
  SearchReplaceEdit,
  DiffEdit,
  SearchReplaceBlock,
  EditOperation,
  EditValidationResult,
  EditValidationError,
  EditValidationWarning,
  EditApplyResult,
  EditStats,
  EditPreview,
  DiffHunk,
  DiffChange,
  BatchEdit,
  BatchEditResult,
  EditorConfig,
} from './types.js';

export { DEFAULT_EDITOR_CONFIG } from './types.js';

// 解析器导出
export {
  parseSearchReplaceBlocks,
  createWholeFileEdit,
  createSearchReplaceEdit,
  createSingleSearchReplaceEdit,
  createDiffEdit,
  parseDiffPatch,
  applyDiffPatch,
  detectEditMode,
  normalizeSearchString,
  findSearchPositions,
  getSearchLineNumbers,
} from './parsers.js';

// 验证器导出
export {
  validateEdit,
  validateEdits,
  areAllEditsValid,
} from './validator.js';

// 应用器导出
export type { ApplyEditOptions } from './applier.js';
export {
  applyEdit,
  applyBatchEdits,
  previewEdit,
  previewBatchEdits,
} from './applier.js';

// 便捷函数
import type { Edit, SearchReplaceBlock, EditApplyResult } from './types.js';
import { createWholeFileEdit, createSearchReplaceEdit, createSingleSearchReplaceEdit } from './parsers.js';
import { applyEdit, type ApplyEditOptions } from './applier.js';

/**
 * 快速写入文件（整文件模式）
 */
export async function writeFile(
  filePath: string,
  content: string,
  options?: ApplyEditOptions
): Promise<EditApplyResult> {
  const edit = createWholeFileEdit(filePath, content);
  return applyEdit(edit, options);
}

/**
 * 快速编辑文件（单块搜索替换）
 */
export async function editFile(
  filePath: string,
  search: string,
  replace: string,
  options?: ApplyEditOptions
): Promise<EditApplyResult> {
  const edit = createSingleSearchReplaceEdit(filePath, search, replace);
  return applyEdit(edit, options);
}

/**
 * 批量搜索替换
 */
export async function editFileMultiple(
  filePath: string,
  blocks: SearchReplaceBlock[],
  options?: ApplyEditOptions
): Promise<EditApplyResult> {
  const edit = createSearchReplaceEdit(filePath, blocks);
  return applyEdit(edit, options);
}

/**
 * 应用任意编辑
 */
export async function apply(
  edit: Edit,
  options?: ApplyEditOptions
): Promise<EditApplyResult> {
  return applyEdit(edit, options);
}
