"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.MagickImageCollection = void 0;
const image_magick_1 = require("./image-magick");
const exception_1 = require("./internal/exception/exception");
const magick_format_1 = require("./magick-format");
const magick_image_1 = require("./magick-image");
const magick_read_settings_1 = require("./settings/magick-read-settings");
const magick_settings_1 = require("./settings/magick-settings");
class MagickImageCollection extends Array {
    constructor() {
        super();
    }
    dispose() {
        let image = this.pop();
        while (image !== undefined) {
            image.dispose();
            image = this.pop();
        }
    }
    read(fileNameOrArray, settings) {
        this.dispose();
        exception_1.Exception.use(exception => {
            const readSettings = MagickImageCollection.createSettings(settings);
            if (fileNameOrArray instanceof Uint8Array) {
                readSettings._use(settings => {
                    const length = fileNameOrArray.byteLength;
                    let data = 0;
                    try {
                        data = image_magick_1.ImageMagick._api._malloc(length);
                        image_magick_1.ImageMagick._api.HEAPU8.set(fileNameOrArray, data);
                        const instances = image_magick_1.ImageMagick._api._MagickImageCollection_ReadBlob(settings._instance, data, 0, length, exception.ptr);
                        this.addImages(instances, readSettings);
                    }
                    finally {
                        if (data !== 0)
                            image_magick_1.ImageMagick._api._free(data);
                    }
                });
            }
            else {
                readSettings._fileName = fileNameOrArray;
                readSettings._use(settings => {
                    const instances = image_magick_1.ImageMagick._api._MagickImageCollection_ReadFile(settings._instance, exception.ptr);
                    this.addImages(instances, readSettings);
                });
            }
        });
    }
    static create() {
        return MagickImageCollection.createObject();
    }
    static _createFromImages(images, settings) {
        const collection = MagickImageCollection.createObject();
        collection.addImages(images, settings);
        return collection;
    }
    _use(func) {
        try {
            return func(this);
        }
        finally {
            this.dispose();
        }
    }
    static createObject() {
        return Object.create(MagickImageCollection.prototype);
    }
    static createSettings(settings) {
        if (settings == null)
            return new magick_settings_1.MagickSettings();
        return new magick_read_settings_1.MagickReadSettings(settings);
    }
    addImages(images, settings) {
        settings.format = magick_format_1.MagickFormat.Unknown;
        let image = images;
        while (image !== 0) {
            const next = image_magick_1.ImageMagick._api._MagickImage_GetNext(image);
            image_magick_1.ImageMagick._api._MagickImage_SetNext(image, 0);
            this.push(magick_image_1.MagickImage._createFromImage(image, settings));
            image = next;
        }
    }
}
exports.MagickImageCollection = MagickImageCollection;
