import { YYYY_MM_DD_HH_MM_SS, toDayJs } from './date.js';
import { BuildCommandToConvertAiToSvgWithInkscapeModel, BuildCommandToExtractMetadataFromImageModel, BuildCommandToProcessImageModel, BuildCommandToReplaceImageColorWithImageMagickModel, BuildCommandToResizeImageWithImageMagickModel, IMAGE_MAGICK_COLOR_SPACE_CONTENT, IMAGE_MAGICK_COMPRESSION_CONTENT, } from './type/index.js';
import { getCommand } from './command.js';
import { replaceFileExtension } from './tool.js';
import { BuildCommandToGenerateGifWithGifsicleModel } from '../shared/index.js';
import _ from 'lodash';
// export function buildCommandToBatchProcessImages() {
//   const cmd = [
//     `mogrify`,
//     `-format`,
//     `jpg`,
//     `-path`,
//     `./new_folder`,
//     `*.png`,
//   ]
//   return [cmd]
// }
export function buildCommandToProcessImage(source) {
    const { input, output, colorCount, colorMatrix, colorSpace, compare, compression, } = BuildCommandToProcessImageModel.parse(source);
    const cmd = getCommand(`convert`);
    let inputPath = input.file.path.match(/\.cr2$/i)
        ? `cr2:${input.file.path}`
        : input.file.path;
    cmd.list.push(`"${inputPath}"`);
    let outputPath = output.file.path
        ? output.file.path
        : replaceFileExtension(input.file.path, `.${output.file.format}`);
    if (compare) {
        cmd.list.push(`-compare`);
    }
    if (colorMatrix) {
        cmd.list.push(`-color-matrix`, `"${colorMatrix.row}x${colorMatrix.column}: ${colorMatrix.value.join(' ')}"`);
    }
    if (colorSpace) {
        cmd.list.push(`-colorspace`, IMAGE_MAGICK_COLOR_SPACE_CONTENT[colorSpace].head);
    }
    if (compression) {
        cmd.list.push(`-compress`, IMAGE_MAGICK_COMPRESSION_CONTENT[compression].head);
    }
    if (colorCount) {
        cmd.list.push(`-colors`, String(colorCount));
    }
    cmd.list.push(`"${outputPath}"`);
    // const cmd = [
    //   `magick`,
    //   `-size`,
    //   `320x85`,
    //   `canvas:none`,
    //   `-font`,
    //   `Bookman-DemiItalic`,
    //   `-pointsize`,
    //   `72`,
    //   `-draw`,
    //   `"text`,
    //   `25,60`,
    //   `\'Magick\'"`,
    //   `-channel`,
    //   `RGBA`,
    //   `-blur`,
    //   `0x6`,
    //   `-fill`,
    //   `darkred`,
    //   `-stroke`,
    //   `magenta`,
    //   `-draw`,
    //   `"text`,
    //   `20,55`,
    //   `\'Magick\'"`,
    //   `fuzzy-magick.png`,
    // ]
    return [cmd];
}
export function buildCommandToExtractMetadataFromImage(source) {
    const { inputPath } = BuildCommandToExtractMetadataFromImageModel.parse(source);
    return [['exiftool', `"${inputPath}"`]];
}
export function buildCommandToWriteMetadataToImage(source) {
    const { inputPath, copyright, creator, license, keywords, artist, originalDate, allDates, creationDate, title, description, } = BuildCommandToExtractMetadataFromImageModel.parse(source);
    const cmd = getCommand('exiftool');
    if (copyright) {
        cmd.list.push(`-rights`, `"${copyright}"`, `-CopyrightNotice`, `"${copyright}"`);
    }
    if (artist) {
        cmd.list.push(`artist`, `"${artist}"`);
    }
    if (originalDate) {
        const od = YYYY_MM_DD_HH_MM_SS(toDayJs(originalDate));
        // 1986:11:05 12:00:00
        cmd.list.push(`-datetimeoriginal`, `"${od}"`);
    }
    if (creationDate) {
        // 1986:11:05 12:00:00
        const od = YYYY_MM_DD_HH_MM_SS(toDayJs(creationDate));
        cmd.list.push(`-createdate`, `"${od}"`);
    }
    if (allDates) {
        const d = YYYY_MM_DD_HH_MM_SS(toDayJs(allDates));
        cmd.list.push(`-AllDates`, d);
    }
    if (creator) {
        cmd.list.push(`-XMP-dc:Creator`, `"${creator}"`);
    }
    if (keywords) {
        cmd.list.push(`-sep`, `", "`, `-keywords`, `"${keywords.join(', ')}"`);
        cmd.list.push(`XMP-xmp:Keywords`, `"${keywords.join(', ')}"`);
    }
    if (license) {
        cmd.list.push(`-XMP-dc:Rights`, `"${license}"`, `-xmp:usageterms`, `"${license}"`);
    }
    if (title) {
        cmd.list.push(`-XMP-dc:Title`, `"${title}"`);
        cmd.list.push(`-XMP-xmp:Title`, `"${title}"`);
    }
    if (description) {
        cmd.list.push(`-XMP-dc:Description`, `"${description}"`);
        cmd.list.push(`-XMP-xmp:Description`, `"${description}"`);
    }
    cmd.list.push(`"${inputPath}"`);
    return [cmd];
}
export function buildCommandToRemoveImageMetadata(source) {
    const { inputPath } = BuildCommandToExtractMetadataFromImageModel.parse(source);
    const cmd = [`exiftool`, `-all=`, `"${inputPath}"`];
    return [cmd];
}
export async function buildCommandToReplaceImageColorWithImageMagick(source) {
    const { inputPath, outputPath, startColor, endColor, fuzz } = BuildCommandToReplaceImageColorWithImageMagickModel.parse(source);
    let cmd = [`convert`, `"${inputPath}"`];
    if (fuzz) {
        cmd.push(`-fuzz`, `${fuzz}%`);
    }
    cmd.push(`-fill`, `"${endColor}"`, `-opaque`, `"${startColor}"`);
    cmd.push(`"${outputPath}"`);
    return [cmd];
}
export async function buildCommandToResizeImageWithImageMagick(source) {
    const { inputPath, outputPath, width, height, stretch, gravity } = BuildCommandToResizeImageWithImageMagickModel.parse(source);
    let scale = '';
    if (width) {
        if (height) {
            scale += `${width}x${height}`;
        }
        else {
            scale += `${width}x`;
        }
    }
    else {
        scale += `x${height}`;
    }
    let resize = `${scale}${stretch ? '!' : ''}`;
    const cmd = [`convert`, `"${inputPath}"`, `-resize`, `${resize}`];
    if (gravity) {
        cmd.push(`-gravity`, `${gravity}`);
    }
    cmd.push(`"${outputPath}"`);
    return [cmd];
}
export function buildCommandToGenerateGifWithGifsicle(source) {
    const input = BuildCommandToGenerateGifWithGifsicleModel.parse(source);
    const cmd = ['gifsicle'];
    if (input.unoptimize) {
        cmd.push(`-U`);
    }
    if (input.width) {
        if (input.height) {
            cmd.push(`--resize`, `${input.width}x${input.height}`);
        }
        else {
            cmd.push(`--resize`, `${input.width}x_`);
        }
    }
    else if (input.height) {
        cmd.push(`--resize`, `_x${input.height}`);
    }
    return [cmd];
}
export async function buildCommandToConvertAIToSVGWithInkscape(source) {
    const { input, output } = BuildCommandToConvertAiToSvgWithInkscapeModel.parse(source);
    const cmd = getCommand(`inkscape`);
    cmd.list.push(`"${input.file.path}"`, `-${process.platform === 'darwin' ? 'o' : 'l'}`, `"${output.file.path}"`);
    return [cmd];
}
export function parseImageMetadataFromExifTool(lines) {
    const metadata = [];
    lines.forEach(line => {
        const pieces = line.split(': ');
        //Is this a line with a meta data pair on it?
        if (pieces.length == 2) {
            const name = _.camelCase(pieces[0].trim());
            let bond = pieces[1].trim();
            const number = parseFloat(bond);
            if (!Number.isNaN(number)) {
                bond = number;
            }
            metadata.push({ name, bond });
        }
    });
    return metadata;
}
//# sourceMappingURL=image.js.map