"use strict";
/**
 * Image Processing Pipeline - Copyright (c) Marcus Cemes
 *
 * This source code is licensed under the MIT license found in the
 * LICENSE file in the root directory of this source tree.
 */
var __importDefault = (this && this.__importDefault) || function (mod) {
    return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.PrimitivePipe = exports.PrimitiveMode = void 0;
const common_1 = require("@ipp/common");
const execa_1 = __importDefault(require("execa"));
const os_1 = require("os");
const path_1 = require("path");
const VENDOR_DIR = (0, path_1.resolve)(__dirname, "..", "vendor");
const SUPPORTED_FORMATS = ["jpeg", "png"];
var PrimitiveMode;
(function (PrimitiveMode) {
    PrimitiveMode[PrimitiveMode["COMBO"] = 0] = "COMBO";
    PrimitiveMode[PrimitiveMode["TRIANGLE"] = 1] = "TRIANGLE";
    PrimitiveMode[PrimitiveMode["RECTANGLE"] = 2] = "RECTANGLE";
    PrimitiveMode[PrimitiveMode["ELLIPSE"] = 3] = "ELLIPSE";
    PrimitiveMode[PrimitiveMode["CIRCLE"] = 4] = "CIRCLE";
    PrimitiveMode[PrimitiveMode["ROTATED_RECTANGLE"] = 5] = "ROTATED_RECTANGLE";
    PrimitiveMode[PrimitiveMode["BEZIERS"] = 6] = "BEZIERS";
    PrimitiveMode[PrimitiveMode["ROTATED_ELLIPSE"] = 7] = "ROTATED_ELLIPSE";
    PrimitiveMode[PrimitiveMode["POLYGON"] = 8] = "POLYGON";
})(PrimitiveMode = exports.PrimitiveMode || (exports.PrimitiveMode = {}));
const DEFAULT_OPTIONS = {
    number: 16,
    mode: PrimitiveMode.TRIANGLE,
    alpha: 128,
    concurrency: 1,
};
/**
 * A wrapper pipe around the primitive algorithm by Michael Fogleman. Generates
 * an SVG vector image using geometric primitives. Not a deterministic algorithm.
 *
 * @author https://github.com/fogleman/primitive
 */
const PrimitivePipe = async (data, options) => {
    const parsedOptions = {
        ...DEFAULT_OPTIONS,
        ...(options || {}),
    };
    const format = data.metadata.current.format;
    if (!SUPPORTED_FORMATS.includes(format))
        throw new common_1.PipeException(`Unsupported image format: "${format}"`);
    const flags = [];
    flags.push("-i", "-");
    flags.push("-o", "-");
    flags.push("-n", parsedOptions.number.toString());
    flags.push("-m", parsedOptions.mode.toString());
    flags.push("-a", parsedOptions.alpha.toString());
    flags.push("-j", parsedOptions.concurrency.toString());
    const executable = getExecutable();
    const { stdout } = await (0, execa_1.default)(executable, flags, { input: data.buffer });
    return {
        buffer: Buffer.from(stdout),
        metadata: {
            ...data.metadata,
            current: {
                ...data.metadata.current,
                format: "svg",
            },
        },
    };
};
exports.PrimitivePipe = PrimitivePipe;
function getExecutable() {
    const a = getArch();
    const p = (0, os_1.platform)();
    const e = p === "win32" ? ".exe" : "";
    return (0, path_1.join)(VENDOR_DIR, `primitive-${p}-${a}${e}`);
}
function getArch() {
    const a = (0, os_1.arch)();
    switch (a) {
        case "x32":
            return "amd";
        case "x64":
            return "amd64";
        default:
            return a;
    }
}
exports.default = exports.PrimitivePipe;
