"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.MagickSettings = exports.NativeMagickSettings = void 0;
const image_magick_1 = require("../image-magick");
const magick_format_1 = require("../magick-format");
const native_instance_1 = require("../internal/native-instance");
const string_1 = require("../internal/native/string");
class NativeMagickSettings extends native_instance_1.NativeInstance {
    constructor(settings) {
        const instance = image_magick_1.ImageMagick._api._MagickSettings_Create();
        const disposeMethod = image_magick_1.ImageMagick._api._MagickSettings_Dispose;
        super(instance, disposeMethod);
        if (settings._fileName !== undefined) {
            string_1._withString(settings._fileName, ptr => {
                image_magick_1.ImageMagick._api._MagickSettings_SetFileName(this._instance, ptr);
            });
        }
        if (settings._quality !== undefined)
            image_magick_1.ImageMagick._api._MagickSettings_SetQuality(this._instance, settings._quality);
        if (settings.backgroundColor !== undefined) {
            settings.backgroundColor._use((ptr) => {
                image_magick_1.ImageMagick._api._MagickSettings_BackgroundColor_Set(this._instance, ptr);
            });
        }
        if (settings.fillColor !== undefined)
            this.setOption('fill', settings.fillColor.toString());
        if (settings.font !== undefined) {
            const fileName = `/fonts/${settings.font}`;
            const stats = image_magick_1.ImageMagick._api.FS.analyzePath(fileName);
            if (!stats.exists) {
                throw `Unable to find a font with the name '${settings.font}', add it with Magick.addFont.`;
            }
            string_1._withString(fileName, ptr => {
                image_magick_1.ImageMagick._api._MagickSettings_Font_Set(this._instance, ptr);
            });
        }
        if (settings.fontPointsize !== undefined)
            image_magick_1.ImageMagick._api._MagickSettings_FontPointsize_Set(this._instance, settings.fontPointsize);
        if (settings.format !== undefined) {
            string_1._withString(settings.format, ptr => {
                image_magick_1.ImageMagick._api._MagickSettings_Format_Set(this._instance, ptr);
            });
        }
        if (settings.strokeColor !== undefined)
            this.setOption('stroke', settings.strokeColor.toString());
        if (settings.strokeWidth !== undefined)
            this.setOption('strokeWidth', settings.strokeWidth.toString());
        for (const option in settings._options)
            this.setOption(option, settings._options[option]);
    }
    setOption(option, value) {
        string_1._withString(option, optionPtr => {
            string_1._withString(value, valuePtr => {
                image_magick_1.ImageMagick._api._MagickSettings_SetOption(this._instance, optionPtr, valuePtr);
            });
        });
    }
}
exports.NativeMagickSettings = NativeMagickSettings;
class MagickSettings {
    constructor() {
        this._options = {};
    }
    getDefine(nameOrFormat, name) {
        var _a, _b;
        if (name !== undefined)
            return (_a = this._options[`${nameOrFormat}:${name}`]) !== null && _a !== void 0 ? _a : null;
        return (_b = this._options[nameOrFormat]) !== null && _b !== void 0 ? _b : null;
    }
    setDefine(nameOrFormat, nameOrValue, value) {
        if (value === undefined) {
            this._options[nameOrFormat] = nameOrValue;
        }
        else {
            const key = this.parseDefine(nameOrFormat, nameOrValue);
            if (typeof value === 'string')
                this._options[key] = value;
            else if (typeof value === 'number')
                this._options[key] = value.toString();
            else
                this._options[key] = value ? 'true' : 'false';
        }
    }
    setDefines(defines) {
        defines.getDefines().forEach(define => {
            if (define !== undefined)
                this.setDefine(define.format, define.name, define.value);
        });
    }
    _clone() {
        const clone = new MagickSettings();
        clone._fileName = this._fileName;
        clone._quality = this._quality;
        clone.format = this.format;
        for (const option in this._options)
            clone._options[option] = this._options[option];
        return clone;
    }
    _use(func) {
        const settings = new NativeMagickSettings(this);
        try {
            return func(settings);
        }
        finally {
            settings.dispose();
        }
    }
    parseDefine(format, name) {
        if (format === magick_format_1.MagickFormat.Unknown)
            return name;
        return `${format}:${name}`;
    }
}
exports.MagickSettings = MagickSettings;
