"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.MapCapturerContext = void 0;
var tslib_1 = require("tslib");
var number_1 = require("../utils/number");
var olExtent = tslib_1.__importStar(require("ol/extent"));
var Feature_1 = tslib_1.__importDefault(require("ol/Feature"));
var Collection_1 = tslib_1.__importDefault(require("ol/Collection"));
var Polygon_1 = tslib_1.__importDefault(require("ol/geom/Polygon"));
var Vector_1 = tslib_1.__importDefault(require("ol/source/Vector"));
var Vector_2 = tslib_1.__importDefault(require("ol/layer/Vector"));
var Translate_1 = tslib_1.__importDefault(require("ol/interaction/Translate"));
var logger_1 = require("../utils/logger");
var MapCapturerContext = /** @class */ (function () {
    function MapCapturerContext(viewer, mapName, rotation) {
        if (rotation === void 0) { rotation = 0; }
        this.viewer = viewer;
        this.mapName = mapName;
        this.rotation = rotation;
        this.features = new Collection_1.default();
        this.mapCapturerSource = new Vector_1.default({
            features: this.features
        });
        this.mapCapturerLayer = new Vector_2.default({
            source: this.mapCapturerSource
        });
        this.intTranslate = new Translate_1.default({
            features: this.features
        });
        this.intTranslate.on("translateend", this.onTranslateEnd.bind(this));
        this.layerName = "".concat(mapName, "_MapCapturer");
    }
    MapCapturerContext.prototype.getRing = function (origin, paperSize, scaleDenominator) {
        var ring = [];
        var factor = scaleDenominator / (this.viewer.getMetersPerUnit() * 1000 * 2);
        ring.push([origin.x - paperSize.w * factor, origin.y - paperSize.h * factor]);
        ring.push([origin.x + paperSize.w * factor, origin.y - paperSize.h * factor]);
        ring.push([origin.x + paperSize.w * factor, origin.y + paperSize.h * factor]);
        ring.push([origin.x - paperSize.w * factor, origin.y + paperSize.h * factor]);
        return [ring];
    };
    MapCapturerContext.prototype.onTranslateEnd = function () {
        if (this.features.getLength() == 1 && this.activeCallback) {
            var box = this.features.item(0);
            var poly = box.getGeometry();
            var coords = poly.getCoordinates()[0];
            var boxCoords = coords.map(function (c) { return "".concat(c[0], ",").concat(c[1]); }).join(",");
            //Process normalized one
            var npoly = poly.clone();
            var ncenter = olExtent.getCenter(npoly.getExtent());
            npoly.rotate((0, number_1.deg2rad)(this.rotation), ncenter);
            var ncoords = npoly.getCoordinates()[0];
            var nBoxCoords = ncoords.map(function (c) { return "".concat(c[0], ",").concat(c[1]); }).join(",");
            this.activeCallback.updateBoxCoords(boxCoords, nBoxCoords);
        }
    };
    MapCapturerContext.prototype.updateBox = function (paperSize, scaleDenominator, rotation) {
        if (this.activeCallback) {
            this.rotation = rotation;
            var poly = void 0;
            if (this.features.getLength() == 0) {
                poly = this.createCaptureBox(paperSize, scaleDenominator, this.rotation);
            }
            else {
                var box = this.features.item(0);
                poly = box.getGeometry();
                var center = olExtent.getCenter(poly.getExtent());
                var origin_1 = {
                    x: center[0],
                    y: center[1]
                };
                var ring = this.getRing(origin_1, paperSize, scaleDenominator);
                poly.setCoordinates(ring);
                poly.rotate(-(0, number_1.deg2rad)(this.rotation), center);
            }
            var coords = poly.getCoordinates()[0];
            var boxCoords = coords.map(function (c) { return "".concat(c[0], ",").concat(c[1]); }).join(",");
            //Process normalized one
            var npoly = poly.clone();
            var ncenter = olExtent.getCenter(npoly.getExtent());
            npoly.rotate((0, number_1.deg2rad)(this.rotation), ncenter);
            var ncoords = npoly.getCoordinates()[0];
            var nBoxCoords = ncoords.map(function (c) { return "".concat(c[0], ",").concat(c[1]); }).join(",");
            this.activeCallback.updateBoxCoords(boxCoords, nBoxCoords);
        }
    };
    MapCapturerContext.prototype.createCaptureBox = function (paperSize, scaleDenominator, rotation) {
        var origin = this.viewer.getCurrentView();
        var ring = this.getRing(origin, paperSize, scaleDenominator);
        var poly = new Polygon_1.default(ring);
        poly.rotate(-(0, number_1.deg2rad)(rotation), [origin.x, origin.y]);
        var box = new Feature_1.default(poly);
        this.features.clear();
        this.features.push(box);
        return poly;
    };
    MapCapturerContext.prototype.getMapName = function () { return this.mapName; };
    MapCapturerContext.prototype.activate = function (callback, paperSize, scaleDenominator, rotation) {
        this.activeCallback = callback;
        (0, logger_1.debug)("Activating map capturer context for ".concat(this.mapName));
        this.viewer.getLayerManager().addLayer(this.layerName, this.mapCapturerLayer, true);
        this.updateBox(paperSize, scaleDenominator, rotation);
        this.viewer.addInteraction(this.intTranslate);
    };
    MapCapturerContext.prototype.deactivate = function () {
        this.activeCallback = undefined;
        (0, logger_1.debug)("De-activating map capturer context for ".concat(this.mapName));
        this.features.clear();
        this.viewer.getLayerManager().removeLayer(this.layerName);
        this.viewer.removeInteraction(this.intTranslate);
    };
    return MapCapturerContext;
}());
exports.MapCapturerContext = MapCapturerContext;
//# sourceMappingURL=map-capturer-context.js.map