import { h, Host, } from "@stencil/core";
import { ariaBooleanToString } from "../../utils/aria";
import { inheritAttributes } from "../../utils/attributes";
import { rOnLoad } from "../../utils/helpers";
export class Button {
    constructor() {
        this.inheritAttributes = {};
        this.isLargestContentPaintDone = false;
        /**
         * The color to use from your application's color palette.
         */
        this.color = 'primary';
        /**
         * The type of button.
         */
        this.elementType = 'button';
        /**
         * If `true`, the user cannot interact with the button.
         */
        this.disabled = false;
        /**
         * Size of the button
         */
        this.size = '';
        /**
         * Specifies where to display the linked URL.
         * Only applies when an `href` is provided.
         */
        this.target = '_self';
        /**
         * If `true` adds a box shadow to improve readability on image background
         * */
        this.shadow = false;
        /**
         * If `true` the width of the buttons is limited
         */
        this.square = false;
        /**
         * If `true` the button has a active theme
         */
        this.isActive = false;
        /**
         * If `true` the button has a full width
         */
        this.expanded = false;
        /**
         * If `true` the button has no padding and a reduced height
         */
        this.flat = false;
        /**
         * If `true` the button is outlined
         */
        this.outlined = false;
        /**
         * If `true` the button is inverted
         */
        this.inverted = false;
        /**
         * If `true` the label is hidden and a loading spinner is shown instead.
         */
        this.loading = false;
        /**
         * If `true` the button is rounded.
         */
        this.rounded = false;
        /**
         * If `true` the button is a popup.
         */
        this.balPopup = undefined;
        /**
         * Name of the left button icon
         */
        this.icon = '';
        /**
         * If `true` the icon turns
         */
        this.iconTurn = false;
        /**
         * Name of the right button icon
         */
        this.iconRight = '';
        /**
         * The label of the button will not break
         */
        this.noWrap = false;
        /**
         * The name of the button, which is submitted with the form data.
         */
        this.name = '';
        /**
         * The value of the button, which is submitted with the form data.
         */
        this.value = '';
        /**
         * A11y attributes for the native button element.
         */
        this.a11yControls = undefined;
        /**
         * A11y attributes for the native button element.
         */
        this.a11yTitle = undefined;
        /**
         * A11y attributes for the native button element.
         */
        this.a11yLabel = undefined;
        /**
         * A11y attributes for the native button element.
         */
        this.a11yHaspopup = undefined;
        this.onFocus = () => {
            this.balFocus.emit();
        };
        this.onBlur = () => {
            this.balBlur.emit();
        };
        this.onClick = (ev) => {
            if (!this.disabled) {
                this.balClick.emit(ev);
                if (this.href !== undefined) {
                    this.balNavigate.emit(ev);
                }
            }
        };
    }
    listenOnClick(ev) {
        if (this.disabled && ev.target && ev.target === this.el) {
            ev.preventDefault();
            ev.stopPropagation();
        }
    }
    componentDidLoad() {
        rOnLoad(() => (this.isLargestContentPaintDone = true));
    }
    componentWillLoad() {
        this.inheritAttributes = inheritAttributes(this.el, [
            'title',
            'aria-label',
            'aria-controls',
            'aria-hidden',
            'tabindex',
            'aria-haspopup',
        ]);
        if (this.el.hasAttribute('bal-popup') && !this.a11yHaspopup) {
            this.a11yHaspopup = 'true';
        }
    }
    componentDidRender() {
        this.balDidRender.emit();
    }
    get isIconInverted() {
        return this.inverted;
    }
    get buttonCssClass() {
        return {
            'button': true,
            [`is-${this.color}`]: true,
            'is-flat': this.flat,
            'is-square': this.square,
            'is-small': this.size === 'small',
            'is-inverted': this.inverted,
            'is-active': this.isActive,
            'is-outlined': this.outlined,
            'is-fullwidth': this.expanded,
            'is-disabled': this.disabled,
            'is-loading': this.loading,
            'is-rounded': this.rounded === true,
            'has-blur-shadow': this.shadow === true,
        };
    }
    get leftIconAttrs() {
        if (!this.icon || this.loading) {
            return {
                style: { display: 'none' },
            };
        }
        return {};
    }
    get leftRightAttrs() {
        if (!this.iconRight || this.loading) {
            return {
                style: { display: 'none' },
            };
        }
        return {};
    }
    get loadingAttrs() {
        if (!this.loading) {
            return {
                style: { display: 'none' },
            };
        }
        return {
            style: { position: 'absolute' },
        };
    }
    handleClick(ev) {
        if (this.disabled) {
            ev.preventDefault();
            ev.stopPropagation();
        }
    }
    render() {
        const { elementType, download, href, rel, target, name, value } = this;
        const TagType = this.href === undefined ? 'button' : 'a';
        const attrs = TagType === 'button'
            ? { type: elementType, name, value }
            : {
                download,
                href,
                rel,
                target,
            };
        const spinnerColor = () => {
            if (this.disabled) {
                return 'blue';
            }
            switch (this.color) {
                case 'primary':
                case 'success':
                case 'warning':
                case 'danger':
                    return this.inverted ? 'blue' : 'white';
                default:
                    return this.inverted ? 'white' : 'blue';
            }
        };
        const ariaAttributes = {
            'title': this.a11yTitle || this.inheritAttributes['title'],
            'aria-label': this.a11yLabel || this.inheritAttributes['aria-label'] || this.a11yTitle || this.inheritAttributes['title'],
            'aria-controls': this.a11yControls || this.inheritAttributes['aria-controls'],
            'aria-haspopup': this.a11yHaspopup || this.inheritAttributes['aria-haspopup'],
        };
        return (h(Host, { onClick: this.handleClick, class: {
                'bal-button': true,
                'control': true,
                'is-fullwidth': this.expanded,
                'is-disabled': this.disabled,
            } }, h(TagType, Object.assign({}, attrs, this.inheritAttributes, { type: this.elementType, class: this.buttonCssClass, part: "native", disabled: this.disabled, onFocus: this.onFocus, onBlur: this.onBlur, onClick: this.onClick, "aria-disabled": ariaBooleanToString(this.disabled), "data-testid": "bal-button" }, ariaAttributes), this.isLargestContentPaintDone && this.loading ? (h("bal-spinner", Object.assign({ color: spinnerColor(), small: true }, this.loadingAttrs, { deactivated: !this.loading }))) : (''), this.isLargestContentPaintDone && this.icon ? (h("bal-icon", Object.assign({}, this.leftIconAttrs, { class: this.square ? '' : 'icon-left', name: this.icon, size: this.square ? this.size : 'small', turn: this.iconTurn, inverted: this.isIconInverted }))) : (''), h("span", { class: {
                'button-label': true,
                'has-no-wrap': this.noWrap,
                'is-small': this.size === 'small',
            }, style: { opacity: this.loading ? '0' : '1' }, "data-testid": "bal-button-label" }, h("slot", null)), this.isLargestContentPaintDone && this.iconRight ? (h("bal-icon", Object.assign({}, this.leftRightAttrs, { class: "icon-right", name: this.iconRight, size: 'small', turn: this.iconTurn, inverted: this.isIconInverted }))) : (''))));
    }
    static get is() { return "bal-button"; }
    static get originalStyleUrls() {
        return {
            "$": ["bal-button.sass"]
        };
    }
    static get styleUrls() {
        return {
            "$": ["bal-button.css"]
        };
    }
    static get properties() {
        return {
            "color": {
                "type": "string",
                "mutable": false,
                "complexType": {
                    "original": "BalProps.BalButtonColor",
                    "resolved": "\"danger\" | \"info\" | \"info-light\" | \"light\" | \"link\" | \"primary\" | \"primary-light\" | \"secondary\" | \"success\" | \"tertiary\" | \"tertiary-green\" | \"tertiary-purple\" | \"tertiary-red\" | \"tertiary-yellow\" | \"text\" | \"warning\"",
                    "references": {
                        "BalProps": {
                            "location": "global",
                            "id": "global::BalProps"
                        }
                    }
                },
                "required": false,
                "optional": false,
                "docs": {
                    "tags": [],
                    "text": "The color to use from your application's color palette."
                },
                "getter": false,
                "setter": false,
                "reflect": false,
                "attribute": "color",
                "defaultValue": "'primary'"
            },
            "elementType": {
                "type": "string",
                "mutable": false,
                "complexType": {
                    "original": "BalProps.BalButtonElementType",
                    "resolved": "\"button\" | \"reset\" | \"submit\"",
                    "references": {
                        "BalProps": {
                            "location": "global",
                            "id": "global::BalProps"
                        }
                    }
                },
                "required": false,
                "optional": false,
                "docs": {
                    "tags": [],
                    "text": "The type of button."
                },
                "getter": false,
                "setter": false,
                "reflect": false,
                "attribute": "element-type",
                "defaultValue": "'button'"
            },
            "disabled": {
                "type": "boolean",
                "mutable": false,
                "complexType": {
                    "original": "boolean",
                    "resolved": "boolean",
                    "references": {}
                },
                "required": false,
                "optional": false,
                "docs": {
                    "tags": [],
                    "text": "If `true`, the user cannot interact with the button."
                },
                "getter": false,
                "setter": false,
                "reflect": true,
                "attribute": "disabled",
                "defaultValue": "false"
            },
            "size": {
                "type": "string",
                "mutable": false,
                "complexType": {
                    "original": "BalProps.BalButtonSize",
                    "resolved": "\"\" | \"small\"",
                    "references": {
                        "BalProps": {
                            "location": "global",
                            "id": "global::BalProps"
                        }
                    }
                },
                "required": false,
                "optional": false,
                "docs": {
                    "tags": [],
                    "text": "Size of the button"
                },
                "getter": false,
                "setter": false,
                "reflect": true,
                "attribute": "size",
                "defaultValue": "''"
            },
            "href": {
                "type": "string",
                "mutable": false,
                "complexType": {
                    "original": "string",
                    "resolved": "string",
                    "references": {}
                },
                "required": false,
                "optional": true,
                "docs": {
                    "tags": [],
                    "text": "Specifies the URL of the page the link goes to"
                },
                "getter": false,
                "setter": false,
                "reflect": false,
                "attribute": "href"
            },
            "target": {
                "type": "string",
                "mutable": false,
                "complexType": {
                    "original": "BalProps.BalButtonTarget",
                    "resolved": "\" _parent\" | \"_blank\" | \"_self\" | \"_top\"",
                    "references": {
                        "BalProps": {
                            "location": "global",
                            "id": "global::BalProps"
                        }
                    }
                },
                "required": false,
                "optional": false,
                "docs": {
                    "tags": [],
                    "text": "Specifies where to display the linked URL.\nOnly applies when an `href` is provided."
                },
                "getter": false,
                "setter": false,
                "reflect": false,
                "attribute": "target",
                "defaultValue": "'_self'"
            },
            "rel": {
                "type": "string",
                "mutable": false,
                "complexType": {
                    "original": "string",
                    "resolved": "string",
                    "references": {}
                },
                "required": false,
                "optional": true,
                "docs": {
                    "tags": [],
                    "text": "Specifies the relationship of the target object to the link object.\nThe value is a space-separated list of [link types](https://developer.mozilla.org/en-US/docs/Web/HTML/Link_types)."
                },
                "getter": false,
                "setter": false,
                "reflect": false,
                "attribute": "rel"
            },
            "download": {
                "type": "string",
                "mutable": false,
                "complexType": {
                    "original": "string",
                    "resolved": "string",
                    "references": {}
                },
                "required": false,
                "optional": true,
                "docs": {
                    "tags": [],
                    "text": "This attribute instructs browsers to download a URL instead of navigating to\nit, so the user will be prompted to save it as a local file. If the attribute\nhas a value, it is used as the pre-filled file name in the Save prompt\n(the user can still change the file name if they want)."
                },
                "getter": false,
                "setter": false,
                "reflect": false,
                "attribute": "download"
            },
            "shadow": {
                "type": "boolean",
                "mutable": false,
                "complexType": {
                    "original": "boolean",
                    "resolved": "boolean",
                    "references": {}
                },
                "required": false,
                "optional": false,
                "docs": {
                    "tags": [],
                    "text": "If `true` adds a box shadow to improve readability on image background"
                },
                "getter": false,
                "setter": false,
                "reflect": false,
                "attribute": "shadow",
                "defaultValue": "false"
            },
            "square": {
                "type": "boolean",
                "mutable": false,
                "complexType": {
                    "original": "boolean",
                    "resolved": "boolean",
                    "references": {}
                },
                "required": false,
                "optional": false,
                "docs": {
                    "tags": [],
                    "text": "If `true` the width of the buttons is limited"
                },
                "getter": false,
                "setter": false,
                "reflect": false,
                "attribute": "square",
                "defaultValue": "false"
            },
            "isActive": {
                "type": "boolean",
                "mutable": false,
                "complexType": {
                    "original": "boolean",
                    "resolved": "boolean",
                    "references": {}
                },
                "required": false,
                "optional": false,
                "docs": {
                    "tags": [],
                    "text": "If `true` the button has a active theme"
                },
                "getter": false,
                "setter": false,
                "reflect": false,
                "attribute": "is-active",
                "defaultValue": "false"
            },
            "expanded": {
                "type": "boolean",
                "mutable": false,
                "complexType": {
                    "original": "boolean",
                    "resolved": "boolean",
                    "references": {}
                },
                "required": false,
                "optional": false,
                "docs": {
                    "tags": [],
                    "text": "If `true` the button has a full width"
                },
                "getter": false,
                "setter": false,
                "reflect": false,
                "attribute": "expanded",
                "defaultValue": "false"
            },
            "flat": {
                "type": "boolean",
                "mutable": false,
                "complexType": {
                    "original": "boolean",
                    "resolved": "boolean",
                    "references": {}
                },
                "required": false,
                "optional": false,
                "docs": {
                    "tags": [],
                    "text": "If `true` the button has no padding and a reduced height"
                },
                "getter": false,
                "setter": false,
                "reflect": false,
                "attribute": "flat",
                "defaultValue": "false"
            },
            "outlined": {
                "type": "boolean",
                "mutable": false,
                "complexType": {
                    "original": "boolean",
                    "resolved": "boolean",
                    "references": {}
                },
                "required": false,
                "optional": false,
                "docs": {
                    "tags": [],
                    "text": "If `true` the button is outlined"
                },
                "getter": false,
                "setter": false,
                "reflect": false,
                "attribute": "outlined",
                "defaultValue": "false"
            },
            "inverted": {
                "type": "boolean",
                "mutable": false,
                "complexType": {
                    "original": "boolean",
                    "resolved": "boolean",
                    "references": {}
                },
                "required": false,
                "optional": false,
                "docs": {
                    "tags": [],
                    "text": "If `true` the button is inverted"
                },
                "getter": false,
                "setter": false,
                "reflect": false,
                "attribute": "inverted",
                "defaultValue": "false"
            },
            "loading": {
                "type": "boolean",
                "mutable": false,
                "complexType": {
                    "original": "boolean",
                    "resolved": "boolean",
                    "references": {}
                },
                "required": false,
                "optional": false,
                "docs": {
                    "tags": [],
                    "text": "If `true` the label is hidden and a loading spinner is shown instead."
                },
                "getter": false,
                "setter": false,
                "reflect": false,
                "attribute": "loading",
                "defaultValue": "false"
            },
            "rounded": {
                "type": "boolean",
                "mutable": false,
                "complexType": {
                    "original": "boolean",
                    "resolved": "boolean",
                    "references": {}
                },
                "required": false,
                "optional": false,
                "docs": {
                    "tags": [],
                    "text": "If `true` the button is rounded."
                },
                "getter": false,
                "setter": false,
                "reflect": false,
                "attribute": "rounded",
                "defaultValue": "false"
            },
            "balPopup": {
                "type": "any",
                "mutable": false,
                "complexType": {
                    "original": "any",
                    "resolved": "any",
                    "references": {}
                },
                "required": false,
                "optional": false,
                "docs": {
                    "tags": [],
                    "text": "If `true` the button is a popup."
                },
                "getter": false,
                "setter": false,
                "reflect": false,
                "attribute": "bal-popup",
                "defaultValue": "undefined"
            },
            "icon": {
                "type": "string",
                "mutable": false,
                "complexType": {
                    "original": "string",
                    "resolved": "string",
                    "references": {}
                },
                "required": false,
                "optional": false,
                "docs": {
                    "tags": [],
                    "text": "Name of the left button icon"
                },
                "getter": false,
                "setter": false,
                "reflect": false,
                "attribute": "icon",
                "defaultValue": "''"
            },
            "iconTurn": {
                "type": "boolean",
                "mutable": false,
                "complexType": {
                    "original": "boolean",
                    "resolved": "boolean",
                    "references": {}
                },
                "required": false,
                "optional": false,
                "docs": {
                    "tags": [],
                    "text": "If `true` the icon turns"
                },
                "getter": false,
                "setter": false,
                "reflect": false,
                "attribute": "icon-turn",
                "defaultValue": "false"
            },
            "iconRight": {
                "type": "string",
                "mutable": false,
                "complexType": {
                    "original": "string",
                    "resolved": "string",
                    "references": {}
                },
                "required": false,
                "optional": false,
                "docs": {
                    "tags": [],
                    "text": "Name of the right button icon"
                },
                "getter": false,
                "setter": false,
                "reflect": false,
                "attribute": "icon-right",
                "defaultValue": "''"
            },
            "noWrap": {
                "type": "boolean",
                "mutable": false,
                "complexType": {
                    "original": "boolean",
                    "resolved": "boolean",
                    "references": {}
                },
                "required": false,
                "optional": false,
                "docs": {
                    "tags": [],
                    "text": "The label of the button will not break"
                },
                "getter": false,
                "setter": false,
                "reflect": false,
                "attribute": "no-wrap",
                "defaultValue": "false"
            },
            "name": {
                "type": "string",
                "mutable": false,
                "complexType": {
                    "original": "string",
                    "resolved": "string",
                    "references": {}
                },
                "required": false,
                "optional": true,
                "docs": {
                    "tags": [],
                    "text": "The name of the button, which is submitted with the form data."
                },
                "getter": false,
                "setter": false,
                "reflect": false,
                "attribute": "name",
                "defaultValue": "''"
            },
            "value": {
                "type": "any",
                "mutable": false,
                "complexType": {
                    "original": "string | number",
                    "resolved": "number | string",
                    "references": {}
                },
                "required": false,
                "optional": true,
                "docs": {
                    "tags": [],
                    "text": "The value of the button, which is submitted with the form data."
                },
                "getter": false,
                "setter": false,
                "reflect": false,
                "attribute": "value",
                "defaultValue": "''"
            },
            "a11yControls": {
                "type": "string",
                "mutable": false,
                "complexType": {
                    "original": "string",
                    "resolved": "string",
                    "references": {}
                },
                "required": false,
                "optional": true,
                "docs": {
                    "tags": [],
                    "text": "A11y attributes for the native button element."
                },
                "getter": false,
                "setter": false,
                "reflect": false,
                "attribute": "a-1-1y-controls",
                "defaultValue": "undefined"
            },
            "a11yTitle": {
                "type": "string",
                "mutable": false,
                "complexType": {
                    "original": "string",
                    "resolved": "string",
                    "references": {}
                },
                "required": false,
                "optional": true,
                "docs": {
                    "tags": [],
                    "text": "A11y attributes for the native button element."
                },
                "getter": false,
                "setter": false,
                "reflect": false,
                "attribute": "a-1-1y-title",
                "defaultValue": "undefined"
            },
            "a11yLabel": {
                "type": "string",
                "mutable": false,
                "complexType": {
                    "original": "string",
                    "resolved": "string",
                    "references": {}
                },
                "required": false,
                "optional": true,
                "docs": {
                    "tags": [],
                    "text": "A11y attributes for the native button element."
                },
                "getter": false,
                "setter": false,
                "reflect": false,
                "attribute": "a-1-1y-label",
                "defaultValue": "undefined"
            },
            "a11yHaspopup": {
                "type": "string",
                "mutable": false,
                "complexType": {
                    "original": "string",
                    "resolved": "string",
                    "references": {}
                },
                "required": false,
                "optional": true,
                "docs": {
                    "tags": [],
                    "text": "A11y attributes for the native button element."
                },
                "getter": false,
                "setter": false,
                "reflect": false,
                "attribute": "a-1-1y-haspopup",
                "defaultValue": "undefined"
            }
        };
    }
    static get states() {
        return {
            "isLargestContentPaintDone": {}
        };
    }
    static get events() {
        return [{
                "method": "balClick",
                "name": "balClick",
                "bubbles": true,
                "cancelable": true,
                "composed": true,
                "docs": {
                    "tags": [],
                    "text": "Emitted when the link element has clicked."
                },
                "complexType": {
                    "original": "BalEvents.BalButtonClickDetail",
                    "resolved": "MouseEvent",
                    "references": {
                        "BalEvents": {
                            "location": "global",
                            "id": "global::BalEvents"
                        }
                    }
                }
            }, {
                "method": "balNavigate",
                "name": "balNavigate",
                "bubbles": true,
                "cancelable": true,
                "composed": true,
                "docs": {
                    "tags": [],
                    "text": "Emitted when the link element has clicked."
                },
                "complexType": {
                    "original": "BalEvents.BalButtonNavigateDetail",
                    "resolved": "MouseEvent",
                    "references": {
                        "BalEvents": {
                            "location": "global",
                            "id": "global::BalEvents"
                        }
                    }
                }
            }, {
                "method": "balFocus",
                "name": "balFocus",
                "bubbles": true,
                "cancelable": true,
                "composed": true,
                "docs": {
                    "tags": [],
                    "text": "Emitted when the button has focus."
                },
                "complexType": {
                    "original": "BalEvents.BalButtonFocusDetail",
                    "resolved": "void",
                    "references": {
                        "BalEvents": {
                            "location": "global",
                            "id": "global::BalEvents"
                        }
                    }
                }
            }, {
                "method": "balBlur",
                "name": "balBlur",
                "bubbles": true,
                "cancelable": true,
                "composed": true,
                "docs": {
                    "tags": [],
                    "text": "Emitted when the button loses focus."
                },
                "complexType": {
                    "original": "BalEvents.BalButtonBlurDetail",
                    "resolved": "void",
                    "references": {
                        "BalEvents": {
                            "location": "global",
                            "id": "global::BalEvents"
                        }
                    }
                }
            }, {
                "method": "balDidRender",
                "name": "balDidRender",
                "bubbles": true,
                "cancelable": true,
                "composed": true,
                "docs": {
                    "tags": [],
                    "text": "Emitted when the button has been  rendered."
                },
                "complexType": {
                    "original": "BalEvents.BalButtonDidRenderDetail",
                    "resolved": "void",
                    "references": {
                        "BalEvents": {
                            "location": "global",
                            "id": "global::BalEvents"
                        }
                    }
                }
            }];
    }
    static get elementRef() { return "el"; }
    static get listeners() {
        return [{
                "name": "click",
                "method": "listenOnClick",
                "target": "document",
                "capture": true,
                "passive": false
            }];
    }
}
