"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, filenamePtr => {
                image_magick_1.ImageMagick._api._MagickSettings_SetFileName(this._instance, filenamePtr);
            });
        }
        if (settings._quality !== undefined) {
            image_magick_1.ImageMagick._api._MagickSettings_SetQuality(this._instance, settings._quality);
        }
        if (settings.format !== undefined) {
            string_1._withString(settings.format, formatPtr => {
                image_magick_1.ImageMagick._api._MagickSettings_Format_Set(this._instance, formatPtr);
            });
        }
        for (const option in settings._options) {
            string_1._withString(option, optionPtr => {
                string_1._withString(settings._options[option], 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;
