'use strict';

var index = require('./index-BEF5cZxd.js');
var utils = require('./utils-NnC78QR3.js');
var sizes = require('./sizes-D5zsw2g5.js');
var template = require('./template-Cj6PcLmR.js');
var props = require('./props-CPENhBnM.js');
var attribute = require('./attribute-CH1-zDDl.js');
var form = require('./form-GgXzZZ-E.js');
var stencil_utils = require('./stencil.utils-8sCYbm34.js');
var helpers_types = require('./helpers.types-vap8QfRH.js');
var string_utils = require('./string.utils-BG4pUfEi.js');
var accessibility = require('./accessibility-BQJmIVWV.js');
require('./gov.log-C5EH15b_.js');
require('./constants-DcKnOyW4.js');
require('./colors-DgrrxjsX.js');
require('./constants-CFAhiwqu.js');
require('./constants-357ehBFU.js');

const FormRangeSizes = utils.pick(sizes.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) {
        index.registerInstance(this, hostRef);
        this.govFocus = index.createEvent(this, "gov-focus");
        this.govBlur = index.createEvent(this, "gov-blur");
        this.govInput = index.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 = template.govHost(this.host);
        this.f = form.govForm(this.h);
        this.inputId = string_utils.createID("GovInput");
    }
    validateSize(newValue) {
        props.validateProp(FormRangeSizes, newValue, FormRangeClass.root);
    }
    watchValue() {
        this.controlOutputPosition();
    }
    watchControlAttrs() {
        this.passControlAttrs();
    }
    passControlAttrs() {
        this.f.passAttrToControl("size", this.size);
        this.f.passAttrToControl("success", template.toBoolAttr(this.success));
        this.f.passAttrToControl("disabled", template.toBoolAttr(this.disabled));
        this.f.passAttrToControl("invalid", template.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 attribute.AttributeObserver(this.host, (attributeName, newValue) => {
            this.onAriaAttributeChange(attributeName, newValue);
        });
        this.attributeObserver.startObserving(attribute.ariaAttributes);
    }
    componentWillLoad() {
        this.validateSize(this.size);
        this.inheritedAttributes = accessibility.inheritAriaAttributes(this.host);
    }
    componentDidRender() {
        this.passControlAttrs();
        this.f.passAttrToLabel("required", String(this.required));
        stencil_utils.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 (index.h(index.Host, { key: '52936cc832e35b7f79520c2f89b2a119e769b994', class: FormRangeClass.root, size: this.size }, this.h.hasSlot("prefix") && index.h("slot", { key: '1f5d3e1d537be5f431cbf4ba366627d82c2daa01', name: "prefix" }), index.h("span", { key: 'cbabb9ff252fbabdd8b2f813de71b31c1a1444e6', class: "element" }, index.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": template.toBoolAttrIfDefined(this.required), "aria-invalid": template.toBoolAttrIfDefined(this.invalid), "aria-disabled": template.toBoolAttrIfDefined(this.disabled) })), index.h("div", { key: 'fff23dd770c71878eae67df3f96b514c3f55ea2e', class: FormRangeClass.fill, ref: el => (this.fillRef = el) }), this.tooltip ?
            index.h("output", { htmlFor: this.name, ref: el => (this.outputRef = el) }, this.value)
            : null, index.h("slot", { key: '32a7919602b2527a97bf019ec74744bedd0ef51f', name: "datalist" })), this.h.hasSlot("sufix") && index.h("slot", { key: 'e9efd215bb9895a6c11666eb694b6c47f8132f12', name: "sufix" }), this.input ?
            index.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 && helpers_types.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 index.getElement(this); }
    static get watchers() { return {
        "size": ["validateSize", "watchControlAttrs"],
        "value": ["watchValue"],
        "disabled": ["watchControlAttrs"],
        "invalid": ["watchControlAttrs"],
        "success": ["watchControlAttrs"]
    }; }
};

exports.gov_form_range = GovFormRange;
//# sourceMappingURL=gov-form-range.entry.cjs.js.map

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