"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.OLStyleMapSet = exports.vectorStyleToStyleMap = void 0;
var tslib_1 = require("tslib");
var ol_style_helpers_1 = require("./ol-style-helpers");
var expr_eval_context_1 = require("./expr-eval-context");
var ol_style_contracts_1 = require("./ol-style-contracts");
var Style_1 = tslib_1.__importDefault(require("ol/style/Style"));
var Circle_1 = tslib_1.__importDefault(require("ol/style/Circle"));
var ol_style_builders_1 = require("./ol-style-builders");
var Icon_1 = tslib_1.__importDefault(require("ol/style/Icon"));
var number_1 = require("../utils/number");
var scoped_id_1 = require("../utils/scoped-id");
var scopedId = new scoped_id_1.ScopedId();
function isDynamicStroke(stroke) {
    if (!stroke) {
        return false;
    }
    return (0, ol_style_contracts_1.isEvaluatable)(stroke.alpha)
        || (0, ol_style_contracts_1.isEvaluatable)(stroke.color)
        || (0, ol_style_contracts_1.isEvaluatable)(stroke.width);
}
function isDynamicFill(fill) {
    if (!fill) {
        return false;
    }
    return (0, ol_style_contracts_1.isEvaluatable)(fill.alpha)
        || (0, ol_style_contracts_1.isEvaluatable)(fill.color);
}
function isDynamicLabel(labelSettings) {
    var label = labelSettings.label;
    if (!label) {
        return false;
    }
    return isDynamicFill(label.backgroundFill)
        || isDynamicStroke(label.backgroundStroke)
        || isDynamicFill(label.fill)
        || (0, ol_style_contracts_1.isEvaluatable)(label.font)
        || (0, ol_style_contracts_1.isEvaluatable)(label.maxAngle)
        || (0, ol_style_contracts_1.isEvaluatable)(label.offsetX)
        || (0, ol_style_contracts_1.isEvaluatable)(label.offsetY)
        || (0, ol_style_contracts_1.isEvaluatable)(label.overflow)
        || (0, ol_style_contracts_1.isEvaluatable)(label.padding)
        || (0, ol_style_contracts_1.isEvaluatable)(label.placement)
        || (0, ol_style_contracts_1.isEvaluatable)(label.rotateWithView)
        || (0, ol_style_contracts_1.isEvaluatable)(label.rotation)
        || (0, ol_style_contracts_1.isEvaluatable)(label.scale)
        || isDynamicStroke(label.stroke)
        || (0, ol_style_contracts_1.isEvaluatable)(label.text)
        || (0, ol_style_contracts_1.isEvaluatable)(label.textAlign)
        || (0, ol_style_contracts_1.isEvaluatable)(label.textBaseline);
}
function isDynamicPointStyle(style) {
    var apts = style;
    var isBaseDynamic = (0, ol_style_contracts_1.isEvaluatable)(apts.radius)
        || isDynamicFill(apts.fill)
        || isDynamicStroke(apts.stroke);
    if (isBaseDynamic) {
        return true;
    }
    return isDynamicLabel(style);
}
function isDynamicLineStyle(style) {
    return isDynamicStroke(style)
        || isDynamicLabel(style);
}
function isDynamicPolygonStyle(style) {
    return isDynamicStroke(style.stroke)
        || isDynamicFill(style.fill)
        || isDynamicLabel(style);
}
function buildStyleMap(pts, lns, pls) {
    //For GeometryCollection, combine point and polygon styles
    var cpts = pts.clone();
    var cpls = pls.clone();
    var gcs = new Style_1.default({
        image: cpts.getImage(),
        stroke: cpls.getStroke(),
        fill: cpls.getFill()
    });
    return {
        id: "".concat(scopedId.next()),
        Point: pts,
        MultiPoint: pts,
        LineString: lns,
        MultiLineString: lns,
        Polygon: pls,
        MultiPolygon: pls,
        Circle: pls,
        GeometryCollection: gcs
    };
}
function vectorStyleToStyleMap(style) {
    var _a, _b, _c;
    var ptStyle = (_a = style.point) !== null && _a !== void 0 ? _a : ol_style_contracts_1.DEFAULT_POINT_CIRCLE_STYLE;
    var lnStyle = (_b = style.line) !== null && _b !== void 0 ? _b : ol_style_contracts_1.DEFAULT_LINE_STYLE;
    var plStyle = (_c = style.polygon) !== null && _c !== void 0 ? _c : ol_style_contracts_1.DEFAULT_POLY_STYLE;
    var builder = function (feat, context) {
        var pts = new Style_1.default();
        if (ptStyle.type == "Circle") {
            pts.setImage(new Circle_1.default({
                radius: (0, ol_style_builders_1.evalFeature)(ptStyle.radius, feat, context),
                fill: (0, ol_style_builders_1.buildFill)(ptStyle.fill, feat, context),
                stroke: (0, ol_style_builders_1.buildStroke)(ptStyle.stroke, feat, context)
            }));
        }
        else {
            pts.setImage(new Icon_1.default({
                anchor: ptStyle.anchor,
                src: (0, ol_style_builders_1.evalFeature)(ptStyle.src, feat, context),
                rotateWithView: (0, ol_style_builders_1.evalFeature)(ptStyle.rotateWithView, feat, context),
                rotation: (0, number_1.deg2rad)((0, ol_style_builders_1.evalFeature)(ptStyle.rotation, feat, context)),
                scale: (0, ol_style_builders_1.evalFeature)(ptStyle.scale, feat, context)
            }));
        }
        var lns = new Style_1.default({
            stroke: (0, ol_style_builders_1.buildStroke)(lnStyle, feat, context)
        });
        var pls = new Style_1.default({
            stroke: (0, ol_style_builders_1.buildStroke)(plStyle.stroke, feat, context),
            fill: (0, ol_style_builders_1.buildFill)(plStyle.fill, feat, context)
        });
        var ptText = (0, ol_style_builders_1.tryBuildTextStyle)(ptStyle, feat, context);
        var lnText = (0, ol_style_builders_1.tryBuildTextStyle)(lnStyle, feat, context);
        var plsText = (0, ol_style_builders_1.tryBuildTextStyle)(plStyle, feat, context);
        if (ptText) {
            pts.setText(ptText);
        }
        if (lnText) {
            lns.setText(lnText);
        }
        if (plsText) {
            pls.setText(plsText);
        }
        return buildStyleMap(pts, lns, pls);
    };
    var isDynamic = isDynamicPointStyle(ptStyle)
        || isDynamicLineStyle(lnStyle)
        || isDynamicPolygonStyle(plStyle);
    if (isDynamic) {
        return builder;
    }
    else {
        return builder(undefined, undefined);
    }
}
exports.vectorStyleToStyleMap = vectorStyleToStyleMap;
/**
 * @since 0.14
 */
var OLStyleMapSet = /** @class */ (function () {
    function OLStyleMapSet(origStyleDef, clusterStyleDef) {
        this.origStyleDef = origStyleDef;
        this.clusterStyleDef = clusterStyleDef;
        this.rules = {
            default: vectorStyleToStyleMap(origStyleDef.default)
        };
        this.exprContext = new expr_eval_context_1.ExprEvalContext();
        for (var k in origStyleDef) {
            if (k != ol_style_helpers_1.DEFAULT_STYLE_KEY && origStyleDef[k]) {
                this.rules[k] = vectorStyleToStyleMap(origStyleDef[k]);
            }
            if (k != ol_style_helpers_1.DEFAULT_STYLE_KEY) {
                this.exprContext.addFilter(k);
            }
        }
        if (clusterStyleDef === null || clusterStyleDef === void 0 ? void 0 : clusterStyleDef.style) {
            var style = clusterStyleDef.style;
            this.clusterRules = {
                default: vectorStyleToStyleMap(style.default)
            };
            for (var k in style) {
                if (k != ol_style_helpers_1.DEFAULT_STYLE_KEY && style[k]) {
                    this.clusterRules[k] = vectorStyleToStyleMap(style[k]);
                }
                if (k != ol_style_helpers_1.DEFAULT_STYLE_KEY) {
                    this.exprContext.addClusterFilter(k);
                }
            }
        }
        this.defaultStyleMap = vectorStyleToStyleMap({});
    }
    OLStyleMapSet.prototype.toVectorLayerStyle = function () {
        return this.origStyleDef;
    };
    OLStyleMapSet.prototype.toClusterSettings = function () {
        return this.clusterStyleDef;
    };
    OLStyleMapSet.prototype.getClusterClickAction = function () {
        var _a;
        return (_a = this.clusterStyleDef) === null || _a === void 0 ? void 0 : _a.onClick;
    };
    OLStyleMapSet.prototype.evaluateStyle = function (feature) {
        var _a, _b;
        try {
            var g = feature.getGeometry();
            if (!g) {
                return null;
            }
            var gt = g.getType();
            var filter = this.exprContext.evaluateFilter(feature);
            var matchingStyle = void 0;
            var r = (0, ol_style_helpers_1.isClusteredFeature)(feature) ? this.clusterRules : this.rules;
            if (filter) {
                matchingStyle = (_a = r === null || r === void 0 ? void 0 : r[filter]) !== null && _a !== void 0 ? _a : this.defaultStyleMap;
            }
            else {
                //Fallback to default
                matchingStyle = (_b = r === null || r === void 0 ? void 0 : r.default) !== null && _b !== void 0 ? _b : this.defaultStyleMap;
            }
            if (typeof (matchingStyle) == 'function') {
                var esm = matchingStyle(feature, this.exprContext);
                return esm[gt];
            }
            else {
                return matchingStyle[gt];
            }
        }
        catch (e) {
            console.error(e);
            return null;
        }
    };
    return OLStyleMapSet;
}());
exports.OLStyleMapSet = OLStyleMapSet;
//# sourceMappingURL=ol-style-map-set.js.map