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.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: '764d66bad7ac6ddb963ae5b06f2356f30454bcfc', class: cls({
                "loading": this.loading || this.isLoading,
                "disabled": this.isDisabled,
                "required": this.required,
                "invalid": !this.isValid,
            }), "data-for": this.name }, h("div", { key: '7705f210d59d43164cfcaab85b2f84567ab6d5a5', class: cls("input-container", {
                "loading": this.loading || this.isLoading,
                "disabled": this.isDisabled,
                "required": this.required,
                "invalid": !this.isValid,
            }) }, h("div", { key: '8f3003ac7e9bc590f9e5efea979b420c278c8d0d', class: "indicator" }, h("slot", { key: '060fddf012796a4554125f617a7c4fda669da3b5', name: "indicator" })), h("div", { key: 'bcdb24104d6130811383981c8c3e1be78b356277', class: "separator" }), h("div", { key: 'b97ba9fbe1b26b2a2bf3a54fe117a9b6c23f79e1', class: "wrap" }, this.label && h("label", { key: '945b4565d672a8147d861180b9bb03bdab33df70', htmlFor: this.inputId }, this.label), h("div", { key: '01f36bc88c80484e583dea4b13a29c234cc838e9' }, h("div", { key: 'cead54c6461d2a884fc9b41a574bc5605c099f0d', class: "slider-background" }), h("div", { key: '9ed7277b582942f60f59e61c9744fa7eaf2e91be', class: "slider-progress" }), h("div", { key: '5b2c60cd7514e48eb17bac17660161c582643e81', class: "slider-tick-container" }), h("input", { key: '53302963a28050051edf5e50f9336ef66769736a', 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: '2fda88bb39aff12679a9f05b43eae1fa3a48409a', class: "separator active" }), h("div", { key: 'c428f195986a2de91b24295147c50ba3f6f58ca7', class: "slider-value-container", style: { minWidth: `${(String(this.max).length) * 9}px` } }, (this.unit && this.unitPosition == 'left') && h("span", { key: '60d1d263db04d85ebc0b212254852e900c397c94' }, this.unit), this.value.toFixed(this.decimals), (this.unit && this.unitPosition == 'right') && h("span", { key: 'bc672487f3eeb32e403d7a8937cf512ceee8b25c' }, this.unit)), this.loading && h("div", { key: '882c5719792904ba2241f8c61e9f71951f06a579', class: "loading-indicator" }), h("div", { key: '04eec543f0a06e5e7375c8392808d58040e2ad85', class: "buttons" }, h("slot", { key: 'f1b43d1fa550535408dd6f4d971fdcc8cea56806', name: "buttons" })), this.info && h("div", { key: '1339b6150a8767582d3d59dc7cb97b6ddc35de0b', class: "info" }, h("x-tooltip", { key: '0bb65ad7317f1b038b79531f1424c40e898855ef', content: this.info }, h("x-icon", { key: '51a347e913c6abc3ddaa69f727d35b1c84af5d36', name: "info" }))), h("slot", { key: '976285a5d0048f82b43f1ae3467c12e79232a9c3' }))));
    }
    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"
            },
            "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"; }
}
