import { Host, h } from "@stencil/core";
import { cls, getId } from "../../utils/helpers";
import { Lang } from "../../services/lang";
export class XSlider {
    constructor() {
        /**
         * Element ID for internal reference
         */
        this.inputId = getId('slider');
        this.name = undefined;
        this.info = undefined;
        this.required = undefined;
        this.loading = undefined;
        this.min = 0;
        this.max = 100;
        this.step = null;
        this.width = undefined;
        this.value = this.min || 0;
        this.label = '';
        this.unit = '';
        this.unitPosition = 'right';
        this.decimals = 0;
        this.isLoading = undefined;
        this.isDisabled = undefined;
        this.isValid = true;
    }
    /**
     * This is called when the 'disabled' attribute of the element or or an ancestor <fieldset> is updated
     *
     * @param disabled
     */
    formDisabledCallback(disabled) {
        this.isDisabled = disabled;
    }
    /**
     * This is called when the parent/owner form is reset
     */
    formResetCallback() {
        this.internals.setFormValue('');
        this.el.dispatchEvent(new InputEvent('input', { bubbles: false }));
    }
    /**
     * This is called when the input is validated
     */
    formValidateCallback(dispatchValidation) {
        if (this.required && Number.isNaN(this.value)) {
            this.internals.setValidity({ valueMissing: true }, Lang.get('fields.validation.value-missing'));
        }
        else if (!Number.isNaN(this.value)) {
            if (!Number.isNaN(this.min) && this.value < this.min) {
                this.internals.setValidity({ rangeUnderflow: true }, Lang.get('fields.validation.range-underflow', { min: this.min }));
            }
            else if (!Number.isNaN(this.max) && this.value > this.max) {
                this.internals.setValidity({ rangeOverflow: true }, Lang.get('fields.validation.range-overflow', { max: this.max }));
            }
            else if (this.step && !Number.isNaN(this.step) && this.value % this.step != 0) {
                this.internals.setValidity({ stepMismatch: true }, Lang.get('fields.validation.step-mismatch'));
            }
        }
        else {
            this.internals.setValidity({});
        }
        if (dispatchValidation) {
            this.isValid = this.internals.validity.valid;
            this.onValidateInputEvent.emit(this.internals);
        }
    }
    /**
     * On input trigger changes event
     *
     * @param e
     */
    onInput(e) {
        var _a;
        const input = e.target;
        this.value = parseFloat(input.value);
        this.internals.setFormValue(input.value);
        this.el.dispatchEvent(new InputEvent('input', { bubbles: false }));
        this.updateProgress();
        this.formValidateCallback(true);
        (_a = this.internals.form) === null || _a === void 0 ? void 0 : _a.dispatchEvent(new InputEvent('input', { bubbles: false }));
    }
    /**
     * This called on input field is unfocused
     */
    onBlurInput() {
        setTimeout(() => this.formValidateCallback(true), 50);
    }
    /**
     * Update the element progress
     */
    updateProgress() {
        const progressEl = this.el.querySelector('.slider-progress');
        const percent = this.getSliderPercent();
        progressEl.style.width = `${percent * 100}%`;
    }
    /**
     * Setup the element ticks
     */
    setTicks() {
        const container = this.el.querySelector(".slider-tick-container");
        const sliderRange = this.max - this.min;
        const tickCount = sliderRange / this.step + 1;
        for (let ii = 0; ii < tickCount; ii++) {
            let tick = document.createElement("span");
            tick.className = "slider-tick";
            container.appendChild(tick);
        }
    }
    /**
     * Get the actual slider percent
     *
     * @returns
     */
    getSliderPercent() {
        const range = this.max - this.min;
        const abs = this.value - this.min;
        return abs / range;
    }
    /**
     * Before mount the element
     */
    componentDidLoad() {
        this.updateProgress();
        if (this.step)
            this.setTicks();
        setTimeout(() => this.formValidateCallback(), 0);
    }
    /**
     * Render element
     */
    render() {
        return (h(Host, { key: '209d9a70c04d48ca332e2320d43da6281704bd58', class: cls({
                "loading": this.loading || this.isLoading,
                "disabled": this.isDisabled,
                "required": this.required,
                "invalid": !this.isValid,
            }), "data-for": this.name, style: { maxWidth: this.width } }, h("div", { key: '714cb6db2ab84b38783c75242c3fec3d88ad24ac', class: cls("input-container", {
                "loading": this.loading || this.isLoading,
                "disabled": this.isDisabled,
                "required": this.required,
                "invalid": !this.isValid,
            }) }, h("div", { key: 'e8585ecd70e8fd979cdc80d9abd33ebb2bc8ab30', class: "indicator" }, h("slot", { key: '02faafd1da96a908b491ceb521c8da9cb891e111', name: "indicator" })), h("div", { key: '37b9035d160d26e29defadf4fbd1e0afb9a899ea', class: "separator" }), h("div", { key: 'eb53a2b5ac09faee9c83b918955d4513634538c8', class: "wrap" }, this.label && h("label", { key: '70d81074c45d57f9c3b310cba08c46763d7f9047', htmlFor: this.inputId }, this.label), h("div", { key: 'ecebb9b729bff886f4404d3d38a8ed2fb3167a8a' }, h("div", { key: '897b5b1cd7ad595a2f0b01e4c269da8915a24e1f', class: "slider-background" }), h("div", { key: '7825f4c0e1a7cde504c2ff09bcccaf7a513e4469', class: "slider-progress" }), h("div", { key: '3fbd9af63c5035b1e5ae62ccae4dd975050eb838', class: "slider-tick-container" }), h("input", { key: '10a105f456e45946d701376c530b7a65f5c43002', id: this.inputId, tabindex: "0", class: "slider-input", type: "range", required: this.required, disabled: this.isDisabled || this.loading || this.isLoading, min: this.min, max: this.max, step: this.step, value: this.value.toString(), onInput: (e) => this.onInput(e) }))), h("div", { key: 'f7b7def089d6d5c94f332a9f87be929e041bad0a', class: "separator active" }), h("div", { key: '013a3d02786be8fd250cf0ac852b012546358a63', class: "slider-value-container", style: { minWidth: `${(String(this.max).length) * 9}px` } }, (this.unit && this.unitPosition == 'left') && h("span", { key: '65659bfcba58943825132af0c1230d63f3ae6ea0' }, this.unit), this.value.toFixed(this.decimals), (this.unit && this.unitPosition == 'right') && h("span", { key: '3bb80358d1276664bcde934fda9c25912313ee32' }, this.unit)), this.loading && h("div", { key: '4b833b582015ecbcab8743e62eb84dc9f1314d50', class: "loading-indicator" }), h("div", { key: 'cfa7220530807f2124142d3d35d759828553ac61', class: "buttons" }, h("slot", { key: 'c55e6b9d27a532af345978f46164fa31a380bc22', name: "buttons" })), this.info && h("div", { key: '044dd572d764d1bfdcc27a67aab24c5f68148ddb', class: "info" }, h("x-tooltip", { key: '72aa595bc965166679c64ede64d3f3264806af2b', content: this.info }, h("x-icon", { key: 'ab7760263f44317c54a808f0f099c3e4c01bab78', name: "info" }))), h("slot", { key: '87094f18c46594c9194a34b731f3b50ba79ea5c1' }))));
    }
    static get is() { return "x-slider"; }
    static get formAssociated() { return true; }
    static get originalStyleUrls() {
        return {
            "$": ["./style.scss"]
        };
    }
    static get styleUrls() {
        return {
            "$": ["style.css"]
        };
    }
    static get properties() {
        return {
            "name": {
                "type": "string",
                "mutable": false,
                "complexType": {
                    "original": "string",
                    "resolved": "string",
                    "references": {}
                },
                "required": false,
                "optional": true,
                "docs": {
                    "tags": [],
                    "text": "Element input name for forms keys"
                },
                "attribute": "name",
                "reflect": false
            },
            "info": {
                "type": "string",
                "mutable": false,
                "complexType": {
                    "original": "string",
                    "resolved": "string",
                    "references": {}
                },
                "required": false,
                "optional": true,
                "docs": {
                    "tags": [],
                    "text": "Element input information block"
                },
                "attribute": "info",
                "reflect": false
            },
            "required": {
                "type": "boolean",
                "mutable": false,
                "complexType": {
                    "original": "boolean",
                    "resolved": "boolean",
                    "references": {}
                },
                "required": false,
                "optional": false,
                "docs": {
                    "tags": [],
                    "text": "Element input required for forms validations"
                },
                "attribute": "required",
                "reflect": false
            },
            "loading": {
                "type": "boolean",
                "mutable": false,
                "complexType": {
                    "original": "boolean",
                    "resolved": "boolean",
                    "references": {}
                },
                "required": false,
                "optional": false,
                "docs": {
                    "tags": [],
                    "text": "Element show loading and block for edit"
                },
                "attribute": "loading",
                "reflect": false
            },
            "min": {
                "type": "number",
                "mutable": false,
                "complexType": {
                    "original": "number",
                    "resolved": "number",
                    "references": {}
                },
                "required": false,
                "optional": true,
                "docs": {
                    "tags": [],
                    "text": "The minimum value of the element"
                },
                "attribute": "min",
                "reflect": false,
                "defaultValue": "0"
            },
            "max": {
                "type": "number",
                "mutable": false,
                "complexType": {
                    "original": "number",
                    "resolved": "number",
                    "references": {}
                },
                "required": false,
                "optional": true,
                "docs": {
                    "tags": [],
                    "text": "The maximum value of the element"
                },
                "attribute": "max",
                "reflect": false,
                "defaultValue": "100"
            },
            "step": {
                "type": "number",
                "mutable": false,
                "complexType": {
                    "original": "number | null",
                    "resolved": "number",
                    "references": {}
                },
                "required": false,
                "optional": true,
                "docs": {
                    "tags": [],
                    "text": "The step value of the element"
                },
                "attribute": "step",
                "reflect": false,
                "defaultValue": "null"
            },
            "width": {
                "type": "string",
                "mutable": false,
                "complexType": {
                    "original": "string",
                    "resolved": "string",
                    "references": {}
                },
                "required": false,
                "optional": true,
                "docs": {
                    "tags": [],
                    "text": "Element input width"
                },
                "attribute": "width",
                "reflect": false
            },
            "value": {
                "type": "number",
                "mutable": true,
                "complexType": {
                    "original": "number | null",
                    "resolved": "number",
                    "references": {}
                },
                "required": false,
                "optional": true,
                "docs": {
                    "tags": [],
                    "text": "The value of the element"
                },
                "attribute": "value",
                "reflect": true,
                "defaultValue": "this.min || 0"
            },
            "label": {
                "type": "string",
                "mutable": false,
                "complexType": {
                    "original": "string",
                    "resolved": "string",
                    "references": {}
                },
                "required": false,
                "optional": true,
                "docs": {
                    "tags": [],
                    "text": "The label of the element"
                },
                "attribute": "label",
                "reflect": false,
                "defaultValue": "''"
            },
            "unit": {
                "type": "string",
                "mutable": false,
                "complexType": {
                    "original": "string",
                    "resolved": "string",
                    "references": {}
                },
                "required": false,
                "optional": true,
                "docs": {
                    "tags": [],
                    "text": "The unit of the element"
                },
                "attribute": "unit",
                "reflect": false,
                "defaultValue": "''"
            },
            "unitPosition": {
                "type": "string",
                "mutable": false,
                "complexType": {
                    "original": "SliderUnitPosition",
                    "resolved": "\"left\" | \"right\"",
                    "references": {
                        "SliderUnitPosition": {
                            "location": "local",
                            "path": "/opt/atlassian/pipelines/agent/build/src/components/x-slider/componente.tsx",
                            "id": "src/components/x-slider/componente.tsx::SliderUnitPosition"
                        }
                    }
                },
                "required": false,
                "optional": false,
                "docs": {
                    "tags": [],
                    "text": "The unit position of the element"
                },
                "attribute": "unit-position",
                "reflect": false,
                "defaultValue": "'right'"
            },
            "decimals": {
                "type": "number",
                "mutable": false,
                "complexType": {
                    "original": "number",
                    "resolved": "number",
                    "references": {}
                },
                "required": false,
                "optional": true,
                "docs": {
                    "tags": [],
                    "text": "The decimals of the element"
                },
                "attribute": "decimals",
                "reflect": false,
                "defaultValue": "0"
            }
        };
    }
    static get states() {
        return {
            "isLoading": {},
            "isDisabled": {},
            "isValid": {}
        };
    }
    static get events() {
        return [{
                "method": "onValidateInputEvent",
                "name": "validated",
                "bubbles": false,
                "cancelable": true,
                "composed": true,
                "docs": {
                    "tags": [],
                    "text": "On validate input event"
                },
                "complexType": {
                    "original": "ElementInternals",
                    "resolved": "ElementInternals",
                    "references": {
                        "ElementInternals": {
                            "location": "global",
                            "id": "global::ElementInternals"
                        }
                    }
                }
            }];
    }
    static get elementRef() { return "el"; }
    static get attachInternalsMemberName() { return "internals"; }
}
