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,EAQA,MAAc,mBAAmB,KAAkC,EAAA;AAEjE,IAAA,OAAO,mBAAmB,KAAK,CAAA;AAAA;AAEnC","file":"file-copier.mjs","sourcesContent":["import * as fs from 'fs';\nimport {minimatch} from 'minimatch';\nimport * as path from 'path';\n\nimport {PathInfo, SearchOptions} from '../types';\nimport {calculateTotalSize, copyFileWithStreams, fileExists} from '../utils/common';\nimport {PathFinder} from './path-finder';\n\n// Types specific to copy functionality\nexport interface CopyOptions {\n  source: string;\n  app?: string;\n  pattern?: string;\n  recursive?: boolean;\n  overwrite?: boolean;\n  dryRun?: boolean;\n  detailed?: boolean;\n  table?: boolean;\n  force?: boolean;\n  interactive?: boolean;\n}\n\nexport interface CopyResult {\n  success: boolean;\n  targetPath: string;\n  copiedFiles: string[];\n  failedFiles: string[];\n  errors: Error[];\n}\n\nexport interface FileAnalysis {\n  source: string;\n  targetPaths: PathInfo[];\n  filesToCopy: string[];\n  totalFiles: number;\n  totalSize: number;\n}\n\nexport class FileCopier {\n  /**\n   * Copy files to iCloud Drive\n   *\n   * @param source Source file or directory path\n   * @param options Copy options\n   * @returns Copy result\n   *\n   * @example\n   * ```typescript\n   * // Simple copy to iCloud Drive root\n   * const result = await FileCopier.copy('./localfile.txt');\n   *\n   * // Copy to specific app with options\n   * const result = await FileCopier.copy('./documents', 'Notes', {\n   *   pattern: '*.md',\n   *   recursive: true,\n   *   overwrite: true\n   * });\n   * ```\n   */\n  static async copy(source: string, options?: Omit<CopyOptions, 'source' | 'app'>): Promise<CopyResult>;\n  static async copy(source: string, target: string, options?: Omit<CopyOptions, 'source' | 'app'>): Promise<CopyResult>;\n  static async copy(\n    source: string,\n    targetOrOptions?: string | Omit<CopyOptions, 'source' | 'app'>,\n    maybeOptions?: Omit<CopyOptions, 'source' | 'app'>,\n  ): Promise<CopyResult> {\n    const copier = new FileCopier();\n\n    // Processing parameters\n    let target: string | undefined;\n    let options: Omit<CopyOptions, 'source'> = {};\n\n    if (typeof targetOrOptions === 'string') {\n      // Call form: copy(source, target, options)\n      target = targetOrOptions;\n      options = maybeOptions || {};\n    } else {\n      // Call form: copy(source, options)\n      options = targetOrOptions || {};\n    }\n\n    return copier.copy({\n      source,\n      app: target,\n      ...options,\n    });\n  }\n\n  /**\n   * Analyze source path and determine files to copy\n   *\n   * @param options Copy options excluding dryRun and overwrite\n   * @returns Analysis result with files to copy and target paths\n   */\n  async analyze(options: Omit<CopyOptions, 'dryRun' | 'overwrite'>): Promise<FileAnalysis> {\n    const sourcePath = path.resolve(options.source);\n    const exists = await fileExists(sourcePath);\n    if (!exists) {\n      throw new Error(`Source path does not exist: ${sourcePath}`);\n    }\n\n    const targetPaths = await this.findTargetPaths(options);\n    if (targetPaths.length === 0) {\n      throw new Error('No valid target paths found');\n    }\n\n    const filesToCopy = await this.findFilesToCopy(sourcePath, options);\n    if (filesToCopy.length === 0) {\n      throw new Error('No files to copy');\n    }\n\n    const totalSize = await calculateTotalSize(filesToCopy);\n\n    return {\n      source: sourcePath,\n      targetPaths,\n      filesToCopy,\n      totalFiles: filesToCopy.length,\n      totalSize,\n    };\n  }\n\n  /**\n   * Copy files to iCloud Drive\n   *\n   * @param options Copy options\n   * @returns Copy result\n   */\n  async copy(options: CopyOptions): Promise<CopyResult>;\n  /**\n   * Copy files to iCloud Drive\n   *\n   * @param source Source file or directory path\n   * @param target Target app name (optional, if not provided files will be copied to iCloud Drive root)\n   * @param options Copy options\n   * @returns Copy result\n   */\n  async copy(source: string, target?: string, options?: Omit<CopyOptions, 'source' | 'app'>): Promise<CopyResult>;\n  async copy(\n    sourceOrOptions: string | CopyOptions,\n    targetOrOptions?: string | Omit<CopyOptions, 'source' | 'app'>,\n    maybeOptions?: Omit<CopyOptions, 'source' | 'app'>,\n  ): Promise<CopyResult> {\n    let options: CopyOptions;\n\n    if (typeof sourceOrOptions === 'string') {\n      // Call form: copy(source, target, options)\n      options = {\n        source: sourceOrOptions,\n        app: typeof targetOrOptions === 'string' ? targetOrOptions : undefined,\n        ...(typeof targetOrOptions === 'string' ? maybeOptions || {} : targetOrOptions || {}),\n      };\n    } else {\n      // Call form: copy(options)\n      options = sourceOrOptions;\n    }\n\n    const analysis = await this.analyze(options);\n    const result: CopyResult = {\n      success: true,\n      targetPath: analysis.targetPaths[0].path,\n      copiedFiles: [],\n      failedFiles: [],\n      errors: [],\n    };\n\n    for (const targetPath of analysis.targetPaths) {\n      for (const sourceFile of analysis.filesToCopy) {\n        const relativePath = path.relative(\n          fs.statSync(analysis.source).isFile() ? path.dirname(analysis.source) : analysis.source,\n          sourceFile,\n        );\n\n        const targetFile = path.join(targetPath.path, relativePath);\n\n        if (!options.dryRun) {\n          try {\n            await this.copyFile(sourceFile, targetFile, options);\n            result.copiedFiles.push(sourceFile);\n          } catch (error: any) {\n            result.success = false;\n            result.failedFiles.push(sourceFile);\n            result.errors.push(error);\n          }\n        } else {\n          result.copiedFiles.push(sourceFile);\n        }\n      }\n    }\n\n    return result;\n  }\n\n  /**\n   * Find target paths based on app name\n   *\n   * @param options Copy options\n   * @returns Array of path info objects\n   */\n  private async findTargetPaths(options: CopyOptions): Promise<PathInfo[]> {\n    const searchOptions: SearchOptions = {\n      appName: options.app,\n      minScore: 10, // Ensure path reliability\n    };\n\n    return PathFinder.find(searchOptions);\n  }\n\n  /**\n   * Find files to copy based on source path and options\n   *\n   * @param sourcePath Source path\n   * @param options Copy options\n   * @returns Array of file paths to copy\n   */\n  private async findFilesToCopy(sourcePath: string, options: CopyOptions): Promise<string[]> {\n    const files: string[] = [];\n    const pattern = options.pattern || '**/*';\n\n    if (!options.recursive && !fs.statSync(sourcePath).isFile()) {\n      throw new Error('Source must be a file when recursive is false');\n    }\n\n    if (fs.statSync(sourcePath).isFile()) {\n      files.push(sourcePath);\n    } else {\n      await this.walkDirectory(sourcePath, pattern, files);\n    }\n\n    return files;\n  }\n\n  /**\n   * Recursively walk directory and collect files matching pattern\n   *\n   * @param dir Directory to walk\n   * @param pattern File pattern to match\n   * @param files Array to collect matching files\n   */\n  private async walkDirectory(dir: string, pattern: string, files: string[]): Promise<void> {\n    const entries = await fs.promises.readdir(dir, {withFileTypes: true});\n\n    for (const entry of entries) {\n      const fullPath = path.join(dir, entry.name);\n\n      if (entry.isDirectory()) {\n        await this.walkDirectory(fullPath, pattern, files);\n      } else if (entry.isFile() && minimatch(entry.name, pattern)) {\n        files.push(fullPath);\n      }\n    }\n  }\n\n  /**\n   * Copy a single file from source to target\n   *\n   * @param source Source file path\n   * @param target Target file path\n   * @param options Copy options\n   */\n  private async copyFile(source: string, target: string, options: CopyOptions): Promise<void> {\n    try {\n      // Use the copyFileWithStreams utility from common.ts\n      await copyFileWithStreams(source, target, options.overwrite);\n    } catch (error: any) {\n      throw new Error(`Failed to copy ${source} to ${target}: ${error.message}`);\n    }\n  }\n\n  /**\n   * Calculate total size of files to copy\n   *\n   * @param files Array of file paths\n   * @returns Total size in bytes\n   */\n  private async calculateTotalSize(files: string[]): Promise<number> {\n    // Use the calculateTotalSize utility from common.ts\n    return calculateTotalSize(files);\n  }\n}\n"]}