import { MicroEditorBase } from "../../../Views/editor/MicroEditorBase.js";
var FormatDataType;
(function (FormatDataType) {
    FormatDataType[FormatDataType["DateTime"] = 0] = "DateTime";
    FormatDataType[FormatDataType["Decimal"] = 1] = "Decimal";
    FormatDataType[FormatDataType["Integer"] = 2] = "Integer";
})(FormatDataType || (FormatDataType = {}));
export class FormatEditor extends MicroEditorBase {
    get inputNode() {
        return this.node.children("input");
    }
    get sampleNode() {
        return this.node.children("div[role=sample]");
    }
    get formatType() {
        return this.source.getFeaturePrimaryValue();
    }
    template() {
        return `<div class="-fb-format-editor">div>
                    <input type="text" />
                    <div role="sample"></
                </div>`;
    }
    onActivate(e) {
        super.onActivate(e);
        this.inputNode.on(Jade.BrowserEvent.KEYUP, (e) => {
            this.input_onKeyUp(e);
        });
        this.inputNode.on(Jade.BrowserEvent.BLUR, (e) => {
            this.input_onBlur(e);
        });
        this.inputNode.on(Jade.BrowserEvent.FOCUS, (e) => {
            this.focus();
        });
        this.inputNode.val(this.formatType.format);
        this.sampleNode.text(this.getSampleValueFormatted());
        this.source.set.onValueChanged((e) => {
            if (e.name == "decimalPlaces") {
                this.formatType.scale = e.value;
                this.sampleNode.text(this.getSampleValueFormatted());
            }
        });
    }
    input_onKeyUp(e) {
        this.sampleNode.text(this.getSampleValueFormatted());
    }
    input_onBlur(e) {
        this.formatType.format = this.inputNode.val();
        this.source.setFeaturePrimaryValue(this.formatType);
    }
    getSampleValue() {
        let result = null;
        let scale = this.formatType.scale;
        switch (this.formatType.type) {
            case FormatDataType.DateTime:
                result = new Date(2010, 9, 31);
                break;
            case FormatDataType.Decimal:
                if (scale > 16) {
                    scale = 16;
                }
                if (scale >= 0) {
                    result = (parseInt("999999999".substr(0, 16 - scale)));
                }
                break;
            case FormatDataType.Integer:
                result = 1750000;
                break;
        }
        return result;
    }
    getSampleValueFormatted() {
        let value = this.getSampleValue();
        let format = this.inputNode.val();
        let scale = this.formatType.scale;
        let result = "";
        switch (this.formatType.type) {
            case FormatDataType.DateTime:
                format = (!format || format === "" ? "d" : format);
                break;
            case FormatDataType.Decimal:
                format = (!format || format === "" || format === "g" ? "n" + scale : format);
                break;
            case FormatDataType.Integer:
                format = (!format || format === "" || format === "g" ? "n0" : format);
                break;
        }
        try {
            result = kendo.toString(value, format.toString());
        }
        catch (ex) {
            result = null;
        }
        if (!result || result === "") {
            this.showError();
            return;
        }
        else {
            this.hideError();
        }
        return result;
    }
    hideError() {
        this.sampleNode.css({
            color: "black",
            backgroundColor: "#dbffce"
        });
    }
    showError() {
        this.sampleNode.text("Invalid Format");
        this.sampleNode.css({
            color: "white",
            backgroundColor: "darkred"
        });
    }
}
