"use strict";
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
    function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
    return new (P || (P = Promise))(function (resolve, reject) {
        function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
        function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
        function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
        step((generator = generator.apply(thisArg, _arguments || [])).next());
    });
};
var __importDefault = (this && this.__importDefault) || function (mod) {
    return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.initializeImageMagick = exports.ImageMagick = void 0;
const magick_js_1 = __importDefault(require("./wasm/magick.js"));
const magick_color_1 = require("./magick-color");
const magick_image_1 = require("./magick-image");
const magick_read_settings_1 = require("./settings/magick-read-settings");
const string_1 = require("./native/string");
class ImageMagick {
    constructor() {
        this.loader = new Promise(resolve => {
            magick_js_1.default().then(api => {
                string_1._withNativeString(api, 'MAGICK_CONFIGURE_PATH', name => {
                    string_1._withNativeString(api, '/xml', value => {
                        api._Environment_SetEnv(name, value);
                        this.api = api;
                    });
                });
                resolve();
            });
        });
    }
    static _create() { return new ImageMagick(); }
    _initialize() {
        return __awaiter(this, void 0, void 0, function* () { yield this.loader; });
    }
    static get _api() {
        if (instance.api === undefined)
            throw new Error('`await initializeImageMagick` should be called to initialize the library');
        return instance.api;
    }
    static set _api(value) {
        instance.api = value;
    }
    static read(fileNameOrArrayOrColor, funcOrSettingsOrWidth, funcOrheight, func) {
        magick_image_1.MagickImage._use((image) => {
            if (fileNameOrArrayOrColor instanceof magick_color_1.MagickColor) {
                if (typeof funcOrSettingsOrWidth === 'number' && typeof funcOrheight === 'number')
                    image.read(fileNameOrArrayOrColor, funcOrSettingsOrWidth, funcOrheight);
                if (func !== undefined)
                    return func(image);
            }
            else if (funcOrSettingsOrWidth instanceof magick_read_settings_1.MagickReadSettings) {
                if (typeof fileNameOrArrayOrColor === 'string')
                    image.read(fileNameOrArrayOrColor, funcOrSettingsOrWidth);
                else
                    image.read(fileNameOrArrayOrColor, funcOrSettingsOrWidth);
                if (funcOrheight !== undefined && typeof funcOrheight !== 'number')
                    return funcOrheight(image);
            }
            else {
                if (typeof fileNameOrArrayOrColor === 'string')
                    image.read(fileNameOrArrayOrColor);
                else
                    image.read(fileNameOrArrayOrColor);
                if (typeof funcOrSettingsOrWidth !== 'number')
                    return funcOrSettingsOrWidth(image);
            }
        });
    }
}
exports.ImageMagick = ImageMagick;
const instance = ImageMagick._create();
function initializeImageMagick() {
    return __awaiter(this, void 0, void 0, function* () { yield instance._initialize(); });
}
exports.initializeImageMagick = initializeImageMagick;
