"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.tryBuildTextStyle = exports.buildFill = exports.buildStroke = exports.evalFeature = void 0;
var tslib_1 = require("tslib");
var ol_style_contracts_1 = require("./ol-style-contracts");
var Text_1 = tslib_1.__importDefault(require("ol/style/Text"));
var Stroke_1 = tslib_1.__importDefault(require("ol/style/Stroke"));
var Fill_1 = tslib_1.__importDefault(require("ol/style/Fill"));
var color_1 = require("ol/color");
function toOLColor(color, alpha) {
    var c = (0, color_1.asArray)(color);
    if (typeof (alpha) != 'undefined') {
        c[3] = (alpha / 255);
    }
    return c;
}
function evalFeature(expr, feat, context) {
    if (!(0, ol_style_contracts_1.isEvaluatable)(expr)) {
        return expr;
    }
    else if (feat && context) {
        try {
            return context.evaluate(expr.expr, feat);
        }
        catch (e) {
            return undefined;
        }
    }
    else {
        return undefined;
    }
}
exports.evalFeature = evalFeature;
function setIfNotUndefined(obj, prop, value) {
    if (typeof (value) != 'undefined') {
        obj[prop] = value;
    }
}
function buildStroke(stroke, feat, context) {
    return new Stroke_1.default({
        color: toOLColor(evalFeature(stroke.color, feat, context), evalFeature(stroke.alpha, feat, context)),
        width: evalFeature(stroke.width, feat, context)
    });
}
exports.buildStroke = buildStroke;
function buildFill(fill, feat, context) {
    return new Fill_1.default({
        color: toOLColor(evalFeature(fill.color, feat, context), evalFeature(fill.alpha, feat, context))
    });
}
exports.buildFill = buildFill;
function tryBuildTextStyle(style, feat, context) {
    var label = style.label;
    if (label) {
        var textOpts = {};
        setIfNotUndefined(textOpts, "font", evalFeature(label.font, feat, context));
        setIfNotUndefined(textOpts, "maxAngle", evalFeature(label.maxAngle, feat, context));
        setIfNotUndefined(textOpts, "offsetX", evalFeature(label.offsetX, feat, context));
        setIfNotUndefined(textOpts, "offsetY", evalFeature(label.offsetY, feat, context));
        setIfNotUndefined(textOpts, "overflow", evalFeature(label.overflow, feat, context));
        setIfNotUndefined(textOpts, "placement", evalFeature(label.placement, feat, context));
        setIfNotUndefined(textOpts, "rotateWithView", evalFeature(label.rotateWithView, feat, context));
        setIfNotUndefined(textOpts, "rotation", evalFeature(label.rotation, feat, context));
        setIfNotUndefined(textOpts, "scale", evalFeature(label.scale, feat, context));
        var txt = evalFeature(label.text, feat, context);
        if (typeof (txt) != 'undefined') {
            textOpts.text = "".concat(txt); //Need to stringify this
        }
        setIfNotUndefined(textOpts, "textAlign", evalFeature(label.textAlign, feat, context));
        setIfNotUndefined(textOpts, "textBaseline", evalFeature(label.textBaseline, feat, context));
        var text = new Text_1.default(textOpts);
        if (label.padding) {
            text.setPadding(label.padding);
        }
        if (label.fill) {
            var f = buildFill(label.fill, feat, context);
            text.setFill(f);
        }
        if (label.backgroundFill) {
            var f = buildFill(label.backgroundFill, feat, context);
            text.setBackgroundFill(f);
        }
        if (label.stroke) {
            var s = buildStroke(label.stroke, feat, context);
            text.setStroke(s);
        }
        if (label.backgroundStroke) {
            var s = buildStroke(label.backgroundStroke, feat, context);
            text.setBackgroundStroke(s);
        }
        return text;
    }
    return undefined;
}
exports.tryBuildTextStyle = tryBuildTextStyle;
//# sourceMappingURL=ol-style-builders.js.map