'use strict';

var index = require('./index-BEF5cZxd.js');
var props = require('./props-CPENhBnM.js');
var constants = require('./constants-z2ZSS2je.js');
var template = require('./template-Cj6PcLmR.js');
var sizes = require('./sizes-D5zsw2g5.js');
var wcag = require('./wcag-Bvzxnp5D.js');
var gov_log = require('./gov.log-C5EH15b_.js');
var utils = require('./utils-NnC78QR3.js');
var accessibility = require('./accessibility-BQJmIVWV.js');
var attribute = require('./attribute-CH1-zDDl.js');
require('./colors-DgrrxjsX.js');
require('./string.utils-BG4pUfEi.js');
require('./helpers.types-vap8QfRH.js');

function toBool(value) {
    if (typeof value === 'string') {
        return ['true', 'True', 'TRUE', '1'].indexOf(value) !== -1;
    }
    else if (typeof value === 'number') {
        return value > 0;
    }
    else if (typeof value === 'boolean') {
        return value;
    }
    else {
        return value ? true : false;
    }
}

var Tags;
(function (Tags) {
    Tags["A"] = "a";
    Tags["BUTTON"] = "button";
})(Tags || (Tags = {}));
const GovButton = class {
    constructor(hostRef) {
        index.registerInstance(this, hostRef);
        this.govClick = index.createEvent(this, "gov-click");
        this.govFocus = index.createEvent(this, "gov-focus");
        this.govBlur = index.createEvent(this, "gov-blur");
        this.hydrated = false;
        /**
         * Button’s size
         */
        this.size = "m";
        /**
         * Expands the button to fill 100% of the container width
         */
        this.expanded = false;
        /**
         * Expands the button to fill 100% of the container width only on mobile
         */
        this.expandedMobile = false;
        /**
         * Makes the button component disabled.
         * This prevents users from being able to interact with the button, and conveys its inactive state to assistive technologies
         */
        this.disabled = false;
        /**
         * Same as original parameter
         * https://developer.mozilla.org/en-US/docs/Web/HTML/Element/button#attr-type
         */
        this.nativeType = "button";
        /**
         * Same as original parameter
         * @link https://developer.mozilla.org/en-US/docs/Web/HTML/Element/a#attr-download
         */
        this.tab = 0;
        /**
         * Same as original parameter
         * @link https://developer.mozilla.org/en-US/docs/Web/HTML/Global_attributes/focusable
         */
        this.focusable = true;
        this.inheritedAttributes = {};
        this.h = template.govHost(this.host);
    }
    validateColor(newValue) {
        props.validateProp(constants.ButtonColors, newValue, constants.ButtonClass.root);
    }
    validateType(newValue) {
        props.validateProp(constants.ButtonType, newValue, constants.ButtonClass.root);
    }
    validateSize(newValue) {
        props.validateProp(sizes.Sizes, newValue, constants.ButtonClass.root);
    }
    validateNativeType(newValue) {
        props.validateProp(constants.NativeType, newValue, constants.ButtonClass.root);
    }
    componentDidLoad() {
        this.attributeObserver = new attribute.AttributeObserver(this.host, (attributeName, newValue) => {
            this.onAriaAttributeChange(attributeName, newValue);
        });
        this.attributeObserver.startObserving(attribute.ariaAttributes);
    }
    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();
    }
    componentWillLoad() {
        this.validateColor(this.color);
        this.validateType(this.type);
        this.validateSize(this.size);
        this.validateNativeType(this.nativeType);
        this.inheritedAttributes = accessibility.inheritAriaAttributes(this.host);
    }
    async componentDidRender() {
        if (gov_log.canValidateWcagOnRender()) {
            await utils.delay(500);
            await this.validateAccessibility();
        }
    }
    connectedCallback() {
        this.hydrated = true;
    }
    render() {
        const Tag = this.href ? Tags.A : Tags.BUTTON;
        const nativeType = Tag === Tags.A ? undefined : this.nativeType;
        return (index.h(index.Host, { key: '0d010913c0331fcb11918152cb9bb2598923c5eb', size: this.size, hydrated: this.hydrated, color: this.color, class: this.h.classes(constants.ButtonClass.root), type: this.type }, index.h(Tag, Object.assign({ key: 'd413645e924857fb5451c8e03c986013d85ddc06', class: "element", ref: el => (this.triggerRef = el), onClick: this.onClickHandler.bind(this), onFocus: this.onFocusHandler.bind(this), onBlur: this.onBlurHandler.bind(this), disabled: this.disabled, id: this.identifier, href: this.href, target: this.target, download: this.download, hreflang: this.hreflang, rel: this.rel, name: this.name, type: nativeType, tabindex: this.disabled ? "-1" : this.tab }, this.inheritedAttributes, { "aria-disabled": template.toBoolAttrIfDefined(this.disabled) }), toBool(this.loading) && !this.h.hasSlot("icon-end") &&
            index.h("gov-icon", { key: '13528203c29059fe167ca9b6f1f9134415517d47', name: "loader", class: "gov-spin-animation", type: "components" }), this.h.hasSlot("icon-start") && index.h("slot", { key: '304d82f1e7136a3f11ab44ce5e5b3166c2be2976', name: "icon-start" }), index.h("slot", { key: '6bc84a79e2acc72405c7b6831951d4984e5a6092' }), this.h.hasSlot("icon-end") && index.h("slot", { key: '044971729141feb76253c649de01db88e9459ccf', name: "icon-end" }), toBool(this.loading) && this.h.hasSlot("icon-end") &&
            index.h("gov-icon", { key: 'b55541b4d8ec377ec0aa9c80596b212306a5c02a', name: "loader", class: "gov-spin-animation", type: "components" }))));
    }
    onClickHandler(e) {
        e.stopPropagation();
        if (this.disabled === false) {
            this.govClick.emit({
                component: constants.ButtonClass.root,
                originalEvent: e,
                ref: this.host,
            });
        }
    }
    onFocusHandler(e) {
        e.stopPropagation();
        this.govFocus.emit({
            component: constants.ButtonClass.root,
            originalEvent: e,
            ref: this.host,
        });
    }
    onBlurHandler(e) {
        e.stopPropagation();
        this.govBlur.emit({
            component: constants.ButtonClass.root,
            originalEvent: e,
            ref: this.host,
        });
    }
    /**
     * Focus button element
     */
    async setFocus() {
        this.triggerRef.focus();
    }
    /**
     * Returns a clickable element instance
     */
    async getRef() {
        return this.triggerRef;
    }
    /**
     * Validate the Accessibility attributes of the component
     */
    async validateAccessibility() {
        if (this.focusable) {
            wcag.validateAccessibleLabel(this.triggerRef.getAttribute('aria-label'), this.triggerRef.getAttribute('aria-labelledby'), constants.ButtonClass.root, this.host);
        }
    }
    get host() { return index.getElement(this); }
    static get watchers() { return {
        "color": ["validateColor"],
        "type": ["validateType"],
        "size": ["validateSize"],
        "nativeType": ["validateNativeType"]
    }; }
};

exports.gov_button = GovButton;
//# sourceMappingURL=gov-button.entry.cjs.js.map

//# sourceMappingURL=gov-button.cjs.entry.js.map