import { __decorate, __metadata } from "tslib";
import { h, Host, } from "@stencil/core";
import { ariaBooleanToString } from "../../../utils/aria";
import { defaultConfig, ListenToConfig } from "../../../utils/config";
import { stopEventBubbling } from "../../../utils/form-input";
import { isEnterKey, isSpaceKey } from "../../../utils/keyboard";
import { Logger } from "../../../utils/log";
import { i18nBalListItemAccordionHead } from "./bal-list-item-accordion-head.i18n";
export class ListItemAccordionHead {
    constructor() {
        this.internalId = `bal-list-item-accordion-head-${ListItemAccordionHeadIds++}`;
        this.ariaState = {};
        this.language = defaultConfig.language;
        /**
         * PUBLIC PROPERTY API
         * ------------------------------------------------------
         */
        /**
         * If `true` the list accordion is open
         */
        this.accordionOpen = false;
        /**
         * Icon name string with value 'plus' on default
         */
        this.icon = 'plus';
        /**
         * EVENT BINDING
         * ------------------------------------------------------
         */
        this.onClick = () => {
            if (this.el.closest('bal-list-item')) {
                this.accordionOpen = !this.accordionOpen;
            }
        };
        this.onKeyDown = (ev) => {
            if (isSpaceKey(ev) || isEnterKey(ev)) {
                stopEventBubbling(ev);
                this.onClick();
            }
        };
    }
    createLogger(log) {
        this.log = log;
    }
    accordionOpenHandler(newValue, oldValue) {
        if (newValue !== oldValue) {
            this.balAccordionChange.emit(this.accordionOpen);
        }
    }
    /**
     * LIFECYCLE
     * ------------------------------------------------------
     */
    connectedCallback() {
        this.accordionOpenHandler(this.accordionOpen, false);
    }
    componentDidRender() {
        this.setLabelledby();
    }
    /**
     * LISTENERS
     * ------------------------------------------------------
     */
    /**
     * @internal define config for the component
     */
    async configChanged(state) {
        this.language = state.language;
    }
    /**
     * PUBLIC METHODS
     * ------------------------------------------------------
     */
    /**
     * @internal
     */
    async setAria(aria) {
        if (aria.controlId && aria.controlId !== this.ariaState.controlId) {
            this.ariaState.controlId = aria.controlId;
        }
    }
    /**
     * PRIVATE METHODS
     * ------------------------------------------------------
     */
    setLabelledby() {
        const bodyEl = this.el.parentElement.querySelector('bal-list-item-accordion-body');
        if (bodyEl) {
            bodyEl.setAria({ labelledby: this.internalId });
        }
    }
    /**
     * RENDER
     * ------------------------------------------------------
     */
    render() {
        return (h(Host, { key: '2f270ca78ffebb2a5b9530bf85c04d454a2958cb', class: {
                'bal-list__item': true,
                'bal-list__item__accordion-head': true,
                'bal-list__item__accordion-head--open': this.accordionOpen,
            }, id: this.internalId, role: "button", tabindex: "0", "aria-expanded": ariaBooleanToString(this.accordionOpen), "aria-controls": this.ariaState.controlId, onClick: this.onClick, onKeyDown: this.onKeyDown }, h("slot", { key: '930c5a8211da0caffb858270a6c86c2fda47ef3f' }), h("bal-list-item-icon", { key: 'f4372a567855758b7a13afa0823acf34d5414c77', right: true }, h("bal-icon", { key: '1bb1f003502e824f9ed698d808d3c51450bf7f12', class: "bal-list__item__accordion-head__icon", name: this.icon, size: "small", turn: this.accordionOpen, title: this.accordionOpen
                ? i18nBalListItemAccordionHead[this.language].close
                : i18nBalListItemAccordionHead[this.language].open }))));
    }
    static get is() { return "bal-list-item-accordion-head"; }
    static get properties() {
        return {
            "accordionOpen": {
                "type": "boolean",
                "mutable": true,
                "complexType": {
                    "original": "boolean",
                    "resolved": "boolean",
                    "references": {}
                },
                "required": false,
                "optional": false,
                "docs": {
                    "tags": [],
                    "text": "If `true` the list accordion is open"
                },
                "getter": false,
                "setter": false,
                "reflect": false,
                "attribute": "accordion-open",
                "defaultValue": "false"
            },
            "icon": {
                "type": "string",
                "mutable": false,
                "complexType": {
                    "original": "BalProps.BalListItemAccordionHeadIcon",
                    "resolved": "\"nav-go-down\" | \"plus\"",
                    "references": {
                        "BalProps": {
                            "location": "global",
                            "id": "global::BalProps"
                        }
                    }
                },
                "required": false,
                "optional": false,
                "docs": {
                    "tags": [],
                    "text": "Icon name string with value 'plus' on default"
                },
                "getter": false,
                "setter": false,
                "reflect": false,
                "attribute": "icon",
                "defaultValue": "'plus'"
            }
        };
    }
    static get states() {
        return {
            "ariaState": {},
            "language": {}
        };
    }
    static get events() {
        return [{
                "method": "balAccordionChange",
                "name": "balAccordionChange",
                "bubbles": true,
                "cancelable": true,
                "composed": true,
                "docs": {
                    "tags": [],
                    "text": "Emitted when the accordion state is changed"
                },
                "complexType": {
                    "original": "BalEvents.BalListAccordionChangeDetail",
                    "resolved": "boolean",
                    "references": {
                        "BalEvents": {
                            "location": "global",
                            "id": "global::BalEvents"
                        }
                    }
                }
            }];
    }
    static get methods() {
        return {
            "configChanged": {
                "complexType": {
                    "signature": "(state: BalConfigState) => Promise<void>",
                    "parameters": [{
                            "name": "state",
                            "type": "BalConfigState",
                            "docs": ""
                        }],
                    "references": {
                        "Promise": {
                            "location": "global",
                            "id": "global::Promise"
                        },
                        "BalConfigState": {
                            "location": "import",
                            "path": "../../../utils/config",
                            "id": "src/utils/config/index.ts::BalConfigState"
                        }
                    },
                    "return": "Promise<void>"
                },
                "docs": {
                    "text": "",
                    "tags": [{
                            "name": "internal",
                            "text": "define config for the component"
                        }]
                }
            },
            "setAria": {
                "complexType": {
                    "signature": "(aria: BalListItemAccordionHeadAria) => Promise<void>",
                    "parameters": [{
                            "name": "aria",
                            "type": "BalListItemAccordionHeadAria",
                            "docs": ""
                        }],
                    "references": {
                        "Promise": {
                            "location": "global",
                            "id": "global::Promise"
                        },
                        "BalListItemAccordionHeadAria": {
                            "location": "local",
                            "path": "/home/runner/work/design-system/design-system/packages/core/src/components/bal-list/bal-list-item-accordion-head/bal-list-item-accordion-head.tsx",
                            "id": "src/components/bal-list/bal-list-item-accordion-head/bal-list-item-accordion-head.tsx::BalListItemAccordionHeadAria"
                        }
                    },
                    "return": "Promise<void>"
                },
                "docs": {
                    "text": "",
                    "tags": [{
                            "name": "internal",
                            "text": undefined
                        }]
                }
            }
        };
    }
    static get elementRef() { return "el"; }
    static get watchers() {
        return [{
                "propName": "accordionOpen",
                "methodName": "accordionOpenHandler"
            }];
    }
}
__decorate([
    Logger('bal-list-item-accordion-head'),
    __metadata("design:type", Function),
    __metadata("design:paramtypes", [Function]),
    __metadata("design:returntype", void 0)
], ListItemAccordionHead.prototype, "createLogger", null);
__decorate([
    ListenToConfig(),
    __metadata("design:type", Function),
    __metadata("design:paramtypes", [Object]),
    __metadata("design:returntype", Promise)
], ListItemAccordionHead.prototype, "configChanged", null);
let ListItemAccordionHeadIds = 0;
