import { z } from 'zod';
import { replaceFileExtension } from '../shared/tool.js';
import path from 'path';
import { BuildBaseFileInputModel, BuildBaseFileInputOutputModel, } from '../shared/index.js';
export const transform_input_output_file = (a, b) => (v, ctx) => {
    if (!v.input.file.path.endsWith(`.${a}`)) {
        ctx.addIssue({
            code: z.ZodIssueCode.custom,
            message: `Input path must have extension .${a}`,
            path: ['input', 'file', 'path'],
        });
    }
    if (!v.output.file.path) {
        if (!v.output.file.format) {
            ctx.addIssue({
                code: z.ZodIssueCode.custom,
                message: `Must specify a an output file path or format.`,
                path: ['output', 'file'],
            });
        }
        else if (v.output.file.format !== b) {
            ctx.addIssue({
                code: z.ZodIssueCode.custom,
                message: `Output format must be ${b}`,
                path: ['output', 'file', 'format'],
            });
        }
        else {
            v.output.file.path = replaceFileExtension(v.input.file.path, `.${v.output.file.format}`);
        }
    }
    else if (!v.output.file.path.endsWith(`.${b}`)) {
        ctx.addIssue({
            code: z.ZodIssueCode.custom,
            message: `Output path must have extension .${b}`,
            path: ['output', 'file', 'path'],
        });
    }
    return v;
};
export const transform_input_output_file_array = (a, b) => (v, ctx) => {
    const inputPathFormat = path
        .extname(v.input.file.path)
        .slice(1);
    if (!a.includes(inputPathFormat)) {
        ctx.addIssue({
            code: z.ZodIssueCode.custom,
            message: `Input path has incorrect extension.`,
            path: ['input', 'file', 'path'],
            params: {
                format: a,
            },
        });
    }
    const outputFormat = b.filter(x => x !== inputPathFormat);
    if (!v.output.file.path) {
        if (!v.output.format) {
            ctx.addIssue({
                code: z.ZodIssueCode.custom,
                message: `Must specify a an output file path or format.`,
                path: ['output'],
            });
        }
        else if (!outputFormat.includes(v.output.format)) {
            ctx.addIssue({
                code: z.ZodIssueCode.custom,
                message: `Output format is incorrect.`,
                path: ['output', 'format'],
                params: {
                    format: b,
                },
            });
        }
        else {
            v.output.file.path = replaceFileExtension(v.input.file.path, `.${v.output.file.format}`);
        }
    }
    else {
        const outputPathFormat = path
            .extname(v.output.file.path)
            .slice(1);
        if (!outputFormat.includes(outputPathFormat)) {
            ctx.addIssue({
                code: z.ZodIssueCode.custom,
                message: `Output path has incorrect extension.`,
                path: ['output', 'file', 'path'],
            });
        }
    }
    return v;
};
export const transform_input_file_output_directory_array = (a) => (v, ctx) => {
    const inputPathFormat = path
        .extname(v.input.file.path)
        .slice(1);
    if (!a.includes(inputPathFormat)) {
        ctx.addIssue({
            code: z.ZodIssueCode.custom,
            message: `Input path has incorrect extension.`,
            path: ['input', 'file', 'path'],
            params: {
                format: a,
            },
        });
    }
    return v;
};
export function buildUseConvert(i, o, tool) {
    return async (task, source) => {
        if (task !== 'convert') {
            return false;
        }
        const input = BuildBaseFileInputOutputModel.parse(source);
        const inputFormat = path
            .extname(input.input.file.path)
            .slice(1)
            .toLowerCase();
        if (!i.includes(inputFormat)) {
            return false;
        }
        const outputFormat = input.output.file.path
            ? path.extname(input.output.file.path).slice(1).toLowerCase()
            : input.output.format
                ? input.output.format.toLowerCase()
                : undefined;
        if (!outputFormat ||
            !o.includes(outputFormat)) {
            return false;
        }
        if (inputFormat === outputFormat) {
            return false;
        }
        if (tool && input.tool && input.tool !== tool) {
            return false;
        }
        return true;
    };
}
export function buildUseSplit(i, tool) {
    return async (task, source) => {
        if (task !== 'split') {
            return false;
        }
        const input = BuildBaseFileInputModel.parse(source);
        const inputFormat = path
            .extname(input.input.file.path)
            .slice(1)
            .toLowerCase();
        if (!i.includes(inputFormat)) {
            return false;
        }
        const outputFormat = path
            .extname(input.output.file.path)
            .slice(1)
            .toLowerCase();
        if (!outputFormat ||
            !i.includes(outputFormat)) {
            return false;
        }
        if (input.tool && input.tool !== tool) {
            return false;
        }
        return true;
    };
}
export function buildUseDecompress(i, tool) {
    return async (task, source) => {
        if (task !== 'decompress') {
            return false;
        }
        const input = BuildBaseFileInputOutputModel.parse(source);
        const inputFormat = path
            .extname(input.input.file.path)
            .slice(1)
            .toLowerCase();
        if (!i.includes(inputFormat)) {
            return false;
        }
        if (input.tool && input.tool !== tool) {
            return false;
        }
        return true;
    };
}
//# sourceMappingURL=base.js.map