import * as fse from "fs-extra";
import { constants } from "node:fs";
import { copyFile } from "node:fs/promises";
import { ensureParentDir } from "../utils/file-operations.js";
/**
 * Create-only strategy: Create file if not exists, never update
 * - Create if not exists
 * - Skip silently if exists (whether identical or different)
 */
export class CreateOnlyStrategy {
    copyFileExclusive;
    name = "create-only";
    /**
     * Create a strategy backed by an atomic exclusive-copy primitive.
     *
     * @param copyFileExclusive - Injectable exclusive copy primitive for tests
     */
    constructor(copyFileExclusive = copyFile) {
        this.copyFileExclusive = copyFileExclusive;
    }
    /**
     * Apply create-only strategy: Create file if not exists, never update
     * @param sourcePath - Source file path
     * @param destPath - Destination file path
     * @param relativePath - Relative path for logging
     * @param context - Strategy context with config and callbacks
     * @returns Result of the create-only operation
     */
    async apply(sourcePath, destPath, relativePath, context) {
        const { config } = context;
        if (config.dryRun) {
            const destExists = await fse.pathExists(destPath);
            return {
                relativePath,
                strategy: this.name,
                action: destExists ? "skipped" : "created",
            };
        }
        await ensureParentDir(destPath);
        try {
            await this.copyFileExclusive(sourcePath, destPath, constants.COPYFILE_EXCL);
            return { relativePath, strategy: this.name, action: "created" };
        }
        catch (error) {
            if (error.code === "EEXIST") {
                return { relativePath, strategy: this.name, action: "skipped" };
            }
            throw error;
        }
    }
}
//# sourceMappingURL=create-only.js.map