import { r as registerInstance, c as createEvent, h, H as Host, g as getElement } from './index-ClISz4NF.js';
import { p as pick } from './utils-CQvUntGY.js';
import { S as Sizes } from './sizes-BcQO4NH2.js';
import { g as govHost, a as toBoolAttr, t as toBoolAttrIfDefined } from './template-CoD1xawt.js';
import { v as validateProp } from './props-CJZgTDla.js';
import { A as AttributeObserver, a as ariaAttributes } from './attribute-Bz_RzYFb.js';
import { g as govForm } from './form-DvhJiKwA.js';
import { t as tick } from './stencil.utils-Cjg0wpDG.js';
import { a as isNumber } from './helpers.types-DNztiAH0.js';
import { c as createID } from './string.utils-vN03FcXN.js';
import { i as inheritAriaAttributes } from './accessibility-wqNV8djF.js';
import './gov.log-BtqD9K8d.js';
import './constants-BegH8u6i.js';
import './colors-U40vKHrs.js';
import './constants-aqe7GX2n.js';
import './constants-CysDgoMA.js';

const FormRangeSizes = pick(Sizes, ['_S', '_M', '_L']);
const FormRangeClass = {
    root: 'gov-form-range',
    fill: 'gov-form-range--fill',
    input: 'gov-form-range--input',
};

const GovFormRange = class {
    constructor(hostRef) {
        registerInstance(this, hostRef);
        this.govFocus = createEvent(this, "gov-focus");
        this.govBlur = createEvent(this, "gov-blur");
        this.govInput = createEvent(this, "gov-input");
        /**
         * Range’s size
         */
        this.size = "m";
        /**
         * Show tooltip with current value
         */
        this.tooltip = false;
        /**
         * Show input with current value
         */
        this.input = false;
        /**
         * Set whether the range is required or not
         */
        this.required = false;
        /**
         * Makes the range component disabled
         */
        this.disabled = false;
        this.inheritedAttributes = {};
        this.h = govHost(this.host);
        this.f = govForm(this.h);
        this.inputId = createID("GovInput");
    }
    validateSize(newValue) {
        validateProp(FormRangeSizes, newValue, FormRangeClass.root);
    }
    watchValue() {
        this.controlOutputPosition();
    }
    watchControlAttrs() {
        this.passControlAttrs();
    }
    passControlAttrs() {
        this.f.passAttrToControl("size", this.size);
        this.f.passAttrToControl("success", toBoolAttr(this.success));
        this.f.passAttrToControl("disabled", toBoolAttr(this.disabled));
        this.f.passAttrToControl("invalid", toBoolAttr(this.invalid));
    }
    get getThumbWidth() {
        const styles = window.getComputedStyle(this.inputRef);
        return parseFloat(styles.getPropertyValue('--thumb-size')) || 1;
    }
    controlOutputPosition() {
        const valueInt = +this.value;
        const min = +this.inputRef.min;
        const max = +this.inputRef.max;
        const thumbWidth = (this.getThumbWidth * 16);
        const rangeWidth = this.inputRef.offsetWidth;
        const percentage = ((valueInt - min) / (max - min)) * 100;
        let tooltipOffset = ((percentage / 100) * (rangeWidth - thumbWidth)) + (thumbWidth / 2);
        if (this.outputRef) {
            this.outputRef.textContent = this.value;
            const outputWidth = this.outputRef.offsetWidth / 2;
            this.outputRef.style.left = `${tooltipOffset - outputWidth}px`;
        }
        if (this.fillRef) {
            this.fillRef.style.width = `${tooltipOffset}px`;
        }
    }
    componentDidLoad() {
        this.attributeObserver = new AttributeObserver(this.host, (attributeName, newValue) => {
            this.onAriaAttributeChange(attributeName, newValue);
        });
        this.attributeObserver.startObserving(ariaAttributes);
    }
    componentWillLoad() {
        this.validateSize(this.size);
        this.inheritedAttributes = inheritAriaAttributes(this.host);
    }
    componentDidRender() {
        this.passControlAttrs();
        this.f.passAttrToLabel("required", String(this.required));
        tick(this.controlOutputPosition.bind(this));
    }
    onAriaAttributeChange(attributeName, newValue) {
        this.host.removeAttribute(attributeName);
        this.inheritedAttributes = Object.assign(Object.assign({}, this.inheritedAttributes), { [attributeName]: newValue });
    }
    disconnectedCallback() {
        var _a;
        (_a = this.attributeObserver) === null || _a === void 0 ? void 0 : _a.stopObserving();
    }
    render() {
        return (h(Host, { key: '52936cc832e35b7f79520c2f89b2a119e769b994', class: FormRangeClass.root, size: this.size }, this.h.hasSlot("prefix") && h("slot", { key: '1f5d3e1d537be5f431cbf4ba366627d82c2daa01', name: "prefix" }), h("span", { key: 'cbabb9ff252fbabdd8b2f813de71b31c1a1444e6', class: "element" }, h("input", Object.assign({ key: '02dd3f62cb518c965c0f5529ae3b213a9c6fd649', type: "range", ref: el => (this.inputRef = el), onInput: this.onInputHandler.bind(this), onFocus: this.onFocusHandler.bind(this), onBlur: this.onBlurHandler.bind(this), id: this.identifier || this.inputId, name: this.name, min: this.min, max: this.max, step: this.step, list: this.listIdentifier, value: this.value, required: this.required, disabled: this.disabled }, this.inheritedAttributes, { "aria-required": toBoolAttrIfDefined(this.required), "aria-invalid": toBoolAttrIfDefined(this.invalid), "aria-disabled": toBoolAttrIfDefined(this.disabled) })), h("div", { key: 'fff23dd770c71878eae67df3f96b514c3f55ea2e', class: FormRangeClass.fill, ref: el => (this.fillRef = el) }), this.tooltip ?
            h("output", { htmlFor: this.name, ref: el => (this.outputRef = el) }, this.value)
            : null, h("slot", { key: '32a7919602b2527a97bf019ec74744bedd0ef51f', name: "datalist" })), this.h.hasSlot("sufix") && h("slot", { key: 'e9efd215bb9895a6c11666eb694b6c47f8132f12', name: "sufix" }), this.input ?
            h("gov-form-input", Object.assign({ "on-gov-input": this.onCustomInputHandler.bind(this), class: FormRangeClass.input, size: this.size, value: this.value, disabled: this.disabled }, this.inheritedAttributes))
            : null));
    }
    onCustomInputHandler(e) {
        const value = e.target.value;
        if (this.step && isNumber(+this.step)) {
            const numValue = +value;
            const numMin = +this.min;
            const numMax = +this.max;
            const numStep = +this.step;
            if (isNaN(numValue)) {
                return false;
            }
            if (numValue < numMin || numValue > numMax) {
                return false;
            }
            const diff = numValue - numMin;
            if (diff % numStep !== 0) {
                return false;
            }
            this.value = value;
            this.govInput.emit({
                component: FormRangeClass.root,
                value: this.value,
                originalEvent: e,
            });
        }
    }
    onInputHandler(e) {
        this.value = e.target.value;
        e.stopPropagation();
        this.govInput.emit({
            component: FormRangeClass.root,
            value: this.value,
            originalEvent: e,
        });
    }
    onFocusHandler(e) {
        e.stopPropagation();
        this.govFocus.emit({
            component: FormRangeClass.root,
            value: this.value,
            originalEvent: e,
        });
    }
    onBlurHandler(e) {
        e.stopPropagation();
        this.govBlur.emit({
            component: FormRangeClass.root,
            value: this.value,
            originalEvent: e,
        });
    }
    /**
     * Returns the current state of the component
     */
    async setFocus(options) {
        return this.inputRef.focus(options);
    }
    /**
     * Sets the ordered value of the element
     */
    async setValue(value) {
        this.value = value;
    }
    /**
     * Clears input value
     */
    async clearValue() {
        this.value = "";
        this.inputRef.value = "";
    }
    /**
     * Returns an instance of the native html input element
     */
    async getRef() {
        return this.inputRef;
    }
    get host() { return getElement(this); }
    static get watchers() { return {
        "size": ["validateSize", "watchControlAttrs"],
        "value": ["watchValue"],
        "disabled": ["watchControlAttrs"],
        "invalid": ["watchControlAttrs"],
        "success": ["watchControlAttrs"]
    }; }
};

export { GovFormRange as gov_form_range };
//# sourceMappingURL=gov-form-range.entry.js.map

//# sourceMappingURL=gov-form-range.entry.js.map