"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.FeatureQueryTooltip = void 0;
var tslib_1 = require("tslib");
var lodash_debounce_1 = tslib_1.__importDefault(require("lodash.debounce"));
var Overlay_1 = tslib_1.__importDefault(require("ol/Overlay"));
var WKT_1 = tslib_1.__importDefault(require("ol/format/WKT"));
var Polygon_1 = require("ol/geom/Polygon");
var client_1 = require("../../api/client");
var olExtent = tslib_1.__importStar(require("ol/extent"));
var error_1 = require("../../api/error");
var i18n_1 = require("../../api/i18n");
var logger_1 = require("../../utils/logger");
var FeatureQueryTooltip = /** @class */ (function () {
    function FeatureQueryTooltip(map, callback) {
        var _this = this;
        this.setLinkClickHandler = function () {
            if (_this.linkElement) {
                _this.linkElement.onclick = _this.handleLinkClick;
            }
        };
        this.handleLinkClick = function (e) {
            e.preventDefault();
            _this.callback.openTooltipLink(e.target.href);
            return false;
        };
        this.callback = callback;
        this.wktFormat = new WKT_1.default();
        this.featureTooltipElement = document.createElement("div");
        this.featureTooltipElement.addEventListener("mouseover", function () { return _this.isMouseOverTooltip = true; });
        this.featureTooltipElement.addEventListener("mouseout", function () { return _this.isMouseOverTooltip = false; });
        this.featureTooltipElement.className = 'feature-tooltip';
        this.featureTooltip = new Overlay_1.default({
            element: this.featureTooltipElement,
            offset: [15, 0],
            positioning: "center-left"
        });
        this.map = map;
        this.map.addOverlay(this.featureTooltip);
        this.throttledMouseMove = (0, lodash_debounce_1.default)(function (e) {
            _this.raiseQueryFromPoint(e.pixel);
        }, 1000);
        this.enabled = true;
        this.isMouseOverTooltip = false;
    }
    FeatureQueryTooltip.prototype.dispose = function () {
        this.featureTooltip.dispose();
    };
    Object.defineProperty(FeatureQueryTooltip.prototype, "isMouseOver", {
        get: function () { return this.isMouseOverTooltip; },
        enumerable: false,
        configurable: true
    });
    FeatureQueryTooltip.prototype.raiseQueryFromPoint = function (pixel) {
        var box = this.callback.getPointSelectionBox(pixel);
        var geom = (0, Polygon_1.fromExtent)(box);
        (0, logger_1.debug)("[".concat(new Date(), "] FeatureTooltip - onMouseMove (").concat(box[0], ", ").concat(box[1], ") (").concat(box[2], ", ").concat(box[3], ")"));
        this.sendTooltipQuery(geom);
    };
    FeatureQueryTooltip.prototype.onMouseMove = function (e) {
        this.throttledMouseMove(e);
    };
    FeatureQueryTooltip.prototype.isEnabled = function () {
        return this.enabled;
    };
    FeatureQueryTooltip.prototype.setEnabled = function (enabled) {
        this.enabled = enabled;
        if (!this.enabled) {
            this.featureTooltipElement.innerHTML = "";
            this.featureTooltipElement.classList.add("tooltip-hidden");
            if (this.linkElement) {
                this.linkElement.onclick = null;
            }
        }
    };
    FeatureQueryTooltip.prototype.sendTooltipQuery = function (geom) {
        var _this = this;
        if (!this.enabled) {
            return;
        }
        if (this.isMouseOverTooltip) {
            (0, logger_1.debug)("Mouse over tooltip. Doing nothing");
            return;
        }
        //const selectedLayerNames = this.onRequestSelectableLayers();
        //if (selectedLayerNames != null && selectedLayerNames.length == 0) {
        //    return;
        //}
        var reqQueryFeatures = 4 | 8; //Tooltips and hyperlinks
        var wkt = this.wktFormat.writeGeometry(geom);
        var client = new client_1.Client(this.callback.getAgentUri(), this.callback.getAgentKind());
        //This is probably a case of blink and you'll miss
        //
        //this.featureTooltipElement.innerHTML = "Querying tooltip data ...";
        //this.featureTooltipElement.classList.remove("tooltip-hidden");
        var coords = olExtent.getCenter(geom.getExtent());
        this.featureTooltip.setPosition(coords);
        //this.callback.incrementBusyWorker();
        client.queryMapFeatures({
            mapname: this.callback.getMapName(),
            session: this.callback.getSessionId(),
            //layernames: selectedLayerNames != null ? selectedLayerNames.join(",") : null,
            geometry: wkt,
            persist: 0,
            selectionvariant: "INTERSECTS",
            maxfeatures: 1,
            requestdata: reqQueryFeatures,
            layerattributefilter: 5
        }).then(function (res) {
            var html = "";
            if (res.Tooltip) {
                html += "<div class='feature-tooltip-body'>".concat(res.Tooltip.replace(/\\n/g, "<br/>"), "</div>");
            }
            if (res.Hyperlink) {
                html += "<div><a id='feature-tooltip-link' href='".concat(res.Hyperlink, "'>").concat((0, i18n_1.tr)("FEATURE_TOOLTIP_URL_HELP_TEXT", _this.callback.getLocale()), "</a></div>");
            }
            _this.featureTooltipElement.innerHTML = html;
            _this.linkElement = document.getElementById("feature-tooltip-link");
            _this.setLinkClickHandler();
            if (html == "") {
                _this.featureTooltipElement.classList.add("tooltip-hidden");
                if (_this.linkElement) {
                    _this.linkElement.onclick = null;
                }
            }
            else {
                _this.featureTooltipElement.classList.remove("tooltip-hidden");
            }
            //this.callback.decrementBusyWorker();
        }).catch(function (err) {
            //this.callback.decrementBusyWorker();
            if ((0, error_1.isSessionExpiredError)(err)) {
                _this.callback.onSessionExpired();
            }
        });
    };
    return FeatureQueryTooltip;
}());
exports.FeatureQueryTooltip = FeatureQueryTooltip;
//# sourceMappingURL=feature.js.map