"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.MagickRectangle = void 0;
const image_magick_1 = require("../image-magick");
const percentage_1 = require("../percentage");
class MagickRectangle {
    static use(image, geometry, func) {
        const rectangle = image_magick_1.ImageMagick._api._MagickRectangle_Create();
        try {
            image_magick_1.ImageMagick._api._MagickRectangle_X_Set(rectangle, geometry.x);
            image_magick_1.ImageMagick._api._MagickRectangle_Y_Set(rectangle, geometry.y);
            let width = geometry.width;
            let height = geometry.height;
            if (geometry.isPercentage) {
                width = new percentage_1.Percentage(geometry.width).multiply(image.width);
                height = new percentage_1.Percentage(geometry.height).multiply(image.height);
            }
            image_magick_1.ImageMagick._api._MagickRectangle_Width_Set(rectangle, width);
            image_magick_1.ImageMagick._api._MagickRectangle_Height_Set(rectangle, height);
            return func(rectangle);
        }
        finally {
            image_magick_1.ImageMagick._api._MagickRectangle_Dispose(rectangle);
        }
    }
}
exports.MagickRectangle = MagickRectangle;
