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.min = 0;
        this.max = 100;
        this.step = undefined;
        this.value = 0;
        this.label = '';
        this.unit = '';
        this.unitPosition = 'right';
        this.decimals = 0;
        this.isLoading = undefined;
        this.isDisabled = undefined;
    }
    /**
     * 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();
    }
    /**
     * 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: 'adbfab90fd7e254a96c44b441afd72aa1fddbced', onResize: () => this.onResize(), class: cls({
                "loading": this.loading || this.isLoading,
                "disabled": this.isDisabled,
                "required": this.required,
            }) }, this.label && h("div", { key: '5b1a5c1c15875d43ab28ccbc2ee82ec0b25e5c84', class: "slider-header" }, h("label", { key: 'c5cc9bc429042d0dfb4b74b21969b2bb6becd59c' }, this.label), h("div", { key: '74a1534b89c5475ea1b4c8cc89098e8af1ea778b', class: "slider-loading" })), h("div", { key: '4f7057c028ca635b0b16e4bf424524ecc5919408', class: "slider-value-container" }, h("div", { key: 'a50f3caf9ee47f194a1daf193cca902e5f197dd0', class: "slider-label slider-min-value" }, (this.unit && this.unitPosition == 'left') && h("span", { key: '3ed961b4c375f23a692da9bc069e10fec1075012' }, this.unit), this.min.toFixed(this.decimals), (this.unit && this.unitPosition == 'right') && h("span", { key: '386e25bb8de2b4167fb40aecea39bbc357b1b663' }, this.unit)), h("div", { key: 'a2ba7c6c7f549c48730c45f6cd89b981b1c203fe', class: "slider-label slider-max-value" }, (this.unit && this.unitPosition == 'left') && h("span", { key: 'b173e59a37cb3b0d91b2cd796eee7c3195527477' }, this.unit), this.max.toFixed(this.decimals), (this.unit && this.unitPosition == 'right') && h("span", { key: '4696564067ac30fa75ccb915fd689b27b6837fa2' }, this.unit)), h("div", { key: 'a024ac75ddf0afa05e482d70c001ac590ae8f345', class: "slider-value" })), h("div", { key: 'ae557150d07b354ec1d013e0b03be40e70b70100', class: "slider-background" }), h("div", { key: '644b187b46988a2add6b5a2b7f0fac620ba3eed2', class: "slider-progress" }), h("div", { key: '966db5e53102b77b95568c7ad69fb7a29e6a48fb', class: "slider-tick-container" }), h("input", { key: '3639e178eeda91a902620ca9368f669294c93582', class: "slider-input", type: "range", name: this.name, 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
            },
            "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
            },
            "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": {}
        };
    }
    static get elementRef() { return "el"; }
    static get attachInternalsMemberName() { return "internals"; }
}
