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: '4c8412f8084ef72e4cf747e75e42420131d8a838', 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: '834448ff588b4d331c72c0f569373461f654423b', class: cls("input-container", {
                "loading": this.loading || this.isLoading,
                "disabled": this.isDisabled,
                "required": this.required,
                "invalid": !this.isValid,
            }) }, h("div", { key: '2f2c72195a25ec54bad397d75642c6d7c3b3188b', class: "indicator" }, h("slot", { key: 'fd3b0dbfea77fc54d6af4b427629519ff76c02a1', name: "indicator" })), h("div", { key: '50daae233f9f8afe84aea7a59defe91fdb2c6496', class: "separator" }), h("div", { key: '7d2bd8ff6579f5c3651c3d4ed61d0557651fca32', class: "wrap" }, this.label && h("label", { key: 'e6084608a7193ee17b105a3bfac90a3ab5f333a4', htmlFor: this.inputId }, this.label), h("div", { key: '70d8423edc65b9d2d55e65063dbf00b77b01e7c3' }, h("div", { key: '89cca00612ba9d12c436730970d2fd6d66dd29b0', class: "slider-background" }), h("div", { key: '2cbf8c0351b7d667e15f60ebeca5ba445b9763a0', class: "slider-progress" }), h("div", { key: 'b5c94f038b8f2672b72f1159badfb0eedca01d74', class: "slider-tick-container" }), h("input", { key: '4b6e11ff6a75d8c04a9be99bb851a60530050460', 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: 'f459a2f7af59c83ab170455dc00ab24609381a71', class: "separator active" }), h("div", { key: 'efbcb42f615871aee668ace413603559b6f2aa47', class: "slider-value-container", style: { minWidth: `${(String(this.max).length) * 9}px` } }, (this.unit && this.unitPosition == 'left') && h("span", { key: '99a6b0cf50482fb50c5de8b6b6581a3a2ad59842' }, this.unit), this.value.toFixed(this.decimals), (this.unit && this.unitPosition == 'right') && h("span", { key: '76dcd84b380964def23e0b76a0e800eca5b1b739' }, this.unit)), this.loading && h("div", { key: '366b80ecacbf7bc2f75871fb48ad534ad4154b08', class: "loading-indicator" }), h("div", { key: '44f325ce1581a3f62b80d8f9fe909e35f82200cd', class: "buttons" }, h("slot", { key: '359f0a943783962631ba8802980902b700c36858', name: "buttons" })), this.info && h("div", { key: '3767675514edd02543165843018e8fddb8293da4', class: "info" }, h("x-tooltip", { key: 'ae04130e6a16216a958d2c9b968fe110cb7d5754', content: this.info }, h("x-icon", { key: '2cba12935782c26c67cd0b145ffae6bffb42e343', name: "info" }))), h("slot", { key: '7750dfa557154d489739ec9d23917c03b0ce8178' }))));
    }
    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"; }
}
