import { Host, h } from "@stencil/core";
import { cls } from "../../utils/helpers";
export class XSlider {
    constructor() {
        this.name = undefined;
        this.required = undefined;
        this.loading = undefined;
        this.validate = undefined;
        this.min = 0;
        this.max = 100;
        this.step = null;
        this.value = 0;
        this.label = '';
        this.unit = '';
        this.unitPosition = 'right';
        this.decimals = 0;
        this.isLoading = undefined;
        this.isDisabled = undefined;
        this.isValid = true;
    }
    /**
     * Validate the input and get the validation result
    */
    async checkValidity() {
        this.onValidate();
        return this.isValid;
    }
    /**
     * 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('');
    }
    /**
     * On input trigger changes event
     *
     * @param e
     */
    onInput(e) {
        const input = e.target;
        this.value = parseFloat(input.value);
        this.internals.setFormValue(input.value);
        this.updateValue();
        this.updateValuePosition();
        this.updateLabels();
        this.updateProgress();
    }
    /**
     * On resize trigger changes event
     */
    onResize() {
        this.updateValuePosition();
    }
    /**
     * This called on input field is unfocused
     */
    onBlurInput() {
        setTimeout(() => this.onValidate(), 50);
    }
    /**
     * Validate the field input
     *
     * @returns
     */
    onValidate() {
        if (!this.validate) {
            return;
        }
        const selectors = ["input", "select", "textarea"];
        let input = null;
        selectors.forEach((selector) => {
            if (input === null) {
                input = this.el.querySelector(selector);
            }
        });
        this.isValid = (input === null || input === void 0 ? void 0 : input.validity) ? input.validity.valid : true;
        this.onValidateInputEvent.emit(input);
    }
    /**
     * Update the element value
     */
    updateValue() {
        if (this.value === null)
            return;
        const valueEl = this.el.querySelector('.slider-value');
        valueEl.innerHTML = `<div>
			${this.unit && this.unitPosition == 'left' ? this.unit : ''}${this.value.toFixed(this.decimals)}${this.unit && this.unitPosition == 'right' ? this.unit : ''}
		</div>`;
    }
    /**
     * Update the element value position
     */
    updateValuePosition() {
        const valueEl = this.el.querySelector('.slider-value');
        const sliderEl = this.el.querySelector('.slider-input');
        const percent = this.getSliderPercent();
        const sliderWidth = sliderEl.getBoundingClientRect().width;
        const valueWidth = valueEl.getBoundingClientRect().width;
        const handleSize = 16;
        let left = percent * (sliderWidth - handleSize) + handleSize / 2 - valueWidth / 2;
        left = Math.min(left, sliderWidth - valueWidth);
        left = sliderEl.value === sliderEl.min ? 0 : left;
        valueEl.style.left = left + "px";
    }
    /**
     * Update the element labels
     */
    updateLabels() {
        const valueEl = this.el.querySelector('.slider-value');
        const minLabel = this.el.querySelector('.slider-min-value');
        const maxLabel = this.el.querySelector('.slider-max-value');
        const valueRect = valueEl.getBoundingClientRect();
        const minLabelRect = minLabel.getBoundingClientRect();
        const maxLabelRect = maxLabel.getBoundingClientRect();
        const minLabelDelta = valueRect.left - (minLabelRect.left);
        const maxLabelDelta = maxLabelRect.left - valueRect.left;
        const deltaThreshold = 32;
        if (minLabelDelta < deltaThreshold)
            minLabel.classList.add("hidden");
        else
            minLabel.classList.remove("hidden");
        if (maxLabelDelta < deltaThreshold)
            maxLabel.classList.add("hidden");
        else
            maxLabel.classList.remove("hidden");
    }
    /**
     * 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.updateValue();
        this.updateValuePosition();
        this.updateLabels();
        this.updateProgress();
        if (this.step)
            this.setTicks();
    }
    /**
     * Render element
     */
    render() {
        return (h(Host, { key: '404dd2f9f0b760d9d83b51f32c383315991a58b1', onResize: () => this.onResize(), class: cls({
                "loading": this.loading || this.isLoading,
                "disabled": this.isDisabled,
                "required": this.required,
                "invalid": !this.isValid,
            }), "data-for": this.name }, this.label && h("div", { key: '8747af8e0372acd0975efa3e29b3862a2f6ee45d', class: "slider-header" }, h("label", { key: 'dd19a6454ca43f4f5327e4f4590f00b49ead6bfc' }, this.label), h("div", { key: '129c40da4a8acce02a571ca5af4f0ff79635bed3', class: "slider-loading" })), h("div", { key: '2a020957290e04379c98a4a4daabb7057f0252fa', class: "slider-value-container" }, h("div", { key: '20dcdda92696a19054907aeddb6b2c5044693456', class: "slider-label slider-min-value" }, (this.unit && this.unitPosition == 'left') && h("span", { key: 'c9242a802ed66e479ee00862243a31c9f87c2871' }, this.unit), this.min.toFixed(this.decimals), (this.unit && this.unitPosition == 'right') && h("span", { key: 'a51438747aa5ba8f6776a4155bf84bce1fd07af6' }, this.unit)), h("div", { key: '5a0447925e342464667fb71efeac808b88612da5', class: "slider-label slider-max-value" }, (this.unit && this.unitPosition == 'left') && h("span", { key: '423a1978ddb603d7e71ac4590190626f310b20bb' }, this.unit), this.max.toFixed(this.decimals), (this.unit && this.unitPosition == 'right') && h("span", { key: 'd229d95457b7730a49ecde64bd8402c467363380' }, this.unit)), h("div", { key: 'e15f39649999db0918977da6aa5cd3aaca0a7f86', class: "slider-value" })), h("div", { key: 'e8476c684290e45a5cd6746790cb4cceb2c032d1', class: "slider-background" }), h("div", { key: '06db61feeaeb4b1ae5e847094265af3554eed75c', class: "slider-progress" }), h("div", { key: 'a8ef6d8013cbcf015254b20dcef7f95a60916c04', class: "slider-tick-container" }), h("input", { key: '9ece7911288a9dc4de60e2c2d5818be770f00cef', 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, onInput: (e) => this.onInput(e) })));
    }
    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
            },
            "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
            },
            "validate": {
                "type": "boolean",
                "mutable": false,
                "complexType": {
                    "original": "boolean",
                    "resolved": "boolean",
                    "references": {}
                },
                "required": false,
                "optional": false,
                "docs": {
                    "tags": [],
                    "text": "Element enable internal validation"
                },
                "attribute": "validate",
                "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": false,
                "defaultValue": "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": true,
                "cancelable": true,
                "composed": true,
                "docs": {
                    "tags": [],
                    "text": "On validate input event"
                },
                "complexType": {
                    "original": "HTMLObjectElement",
                    "resolved": "HTMLObjectElement",
                    "references": {
                        "HTMLObjectElement": {
                            "location": "global",
                            "id": "global::HTMLObjectElement"
                        }
                    }
                }
            }];
    }
    static get methods() {
        return {
            "checkValidity": {
                "complexType": {
                    "signature": "() => Promise<boolean>",
                    "parameters": [],
                    "references": {
                        "Promise": {
                            "location": "global",
                            "id": "global::Promise"
                        }
                    },
                    "return": "Promise<boolean>"
                },
                "docs": {
                    "text": "Validate the input and get the validation result",
                    "tags": []
                }
            }
        };
    }
    static get elementRef() { return "el"; }
    static get attachInternalsMemberName() { return "internals"; }
}
