import { rm } from 'node:fs/promises';
import { compareVersions } from '../../../init/tools/shared/version.js';
import { getSkillVersion } from '../../../shared/frontmatter.js';
import { copyDirectory, copySingleFile, dirExists, fileExists } from '../../../../fs/io.js';
export async function pathExists(path) {
    return (await fileExists(path)) || (await dirExists(path));
}
export async function copyDirWithStatus(source, destination, force) {
    const exists = await pathExists(destination);
    if (exists && !force) {
        return 'skipped';
    }
    if (exists && force) {
        await rm(destination, { recursive: true, force: true });
        await copyDirectory(source, destination);
        return 'updated';
    }
    await copyDirectory(source, destination);
    return 'copied';
}
export async function copyFileWithStatus(source, destination, force) {
    const exists = await pathExists(destination);
    if (exists && !force) {
        return 'skipped';
    }
    if (exists && force) {
        await rm(destination, { recursive: true, force: true });
        await copySingleFile(source, destination);
        return 'updated';
    }
    await copySingleFile(source, destination);
    return 'copied';
}
export async function copyDirWithVersionCheck(source, destination, force) {
    const exists = await pathExists(destination);
    if (!exists) {
        await copyDirectory(source, destination);
        return { status: 'copied' };
    }
    if (force) {
        await rm(destination, { recursive: true, force: true });
        await copyDirectory(source, destination);
        return { status: 'updated' };
    }
    const bundledVersion = await getSkillVersion(source);
    const installedVersion = await getSkillVersion(destination);
    const versionState = compareVersions(installedVersion, bundledVersion);
    if (versionState === 'outdated') {
        return { status: 'outdated', installedVersion, bundledVersion };
    }
    return { status: 'skipped' };
}
