import { __decorate, __metadata } from "tslib";
import { h, Host } from "@stencil/core";
import { isEnterKey, isSpaceKey } from "../../..//utils/keyboard";
import { BEM } from "../../../utils/bem";
import { stopEventBubbling } from "../../../utils/form-input";
import { Logger } from "../../../utils/log";
export class AccordionSummary {
    constructor() {
        this.componentId = `bal-accordion-summary-${accordionSummaryIds++}`;
        /**
         * PUBLIC PROPERTY API
         * ------------------------------------------------------
         */
        /**
         * If `true` the whole summary component acts as a trigger and can be clicked
         */
        this.trigger = false;
        /**
         * @internal
         */
        this.active = false;
        /**
         * @internal
         */
        this.state = 1 /* AccordionState.Collapsed */;
        /**
         * PRIVATE METHODS
         * ------------------------------------------------------
         */
        this.updateAccordionId = () => { var _a; return (this.parentAccordionId = (_a = this.parentAccordionElement) === null || _a === void 0 ? void 0 : _a.id); };
        /**
         * EVENT HANDLER
         * ------------------------------------------------------
         */
        this.onClick = (ev) => {
            var _a;
            stopEventBubbling(ev);
            (_a = this.parentAccordionElement) === null || _a === void 0 ? void 0 : _a.humanToggle();
        };
        this.onKeyDown = (ev) => {
            var _a;
            if (isEnterKey(ev) || isSpaceKey(ev)) {
                stopEventBubbling(ev);
                (_a = this.parentAccordionElement) === null || _a === void 0 ? void 0 : _a.humanToggle();
            }
        };
    }
    createLogger(log) {
        this.log = log;
    }
    /**
     * LIFECYCLE
     * ------------------------------------------------------
     */
    connectedCallback() {
        const accordion = this.parentAccordionElement;
        if (accordion) {
            accordion.version = 2;
        }
        this.updateAccordionId();
    }
    disconnectedCallback() {
        const accordion = this.parentAccordionElement;
        if (accordion) {
            accordion.version = 1;
        }
        this.updateAccordionId();
    }
    /**
     * GETTERS
     * ------------------------------------------------------
     */
    get parentAccordionElement() {
        var _a;
        return ((_a = this.el) === null || _a === void 0 ? void 0 : _a.closest('bal-accordion')) || null;
    }
    /**
     * RENDER
     * ------------------------------------------------------
     */
    render() {
        const block = BEM.block('accordion').element('summary');
        const id = this.parentAccordionId ? `${this.parentAccordionId}-summary` : this.componentId;
        const expanded = this.state === 4 /* AccordionState.Expanded */ || this.state === 8 /* AccordionState.Expanding */;
        const buttonPart = expanded ? 'button expanded' : 'button';
        let attributes = {};
        if (this.trigger) {
            attributes = {
                'aria-controls': `${this.parentAccordionId}-details-content`,
                'role': 'button',
                'tabindex': 0,
                'part': buttonPart,
                'data-testid': 'bal-accordion-button',
                'onClick': this.onClick,
                'onKeyDown': this.onKeyDown,
            };
        }
        return (h(Host, Object.assign({ key: 'ed4f3e46644cf6288855154214126231e5b25e1d', id: id, class: Object.assign(Object.assign(Object.assign(Object.assign({}, block.class()), block.modifier('trigger').class(this.trigger)), block.modifier('inner-trigger').class(!this.trigger)), { 'bal-focused': this.trigger }) }, attributes, { "data-testid": "bal-accordion-summary" })));
    }
    static get is() { return "bal-accordion-summary"; }
    static get originalStyleUrls() {
        return {
            "$": ["bal-accordion-summary.sass"]
        };
    }
    static get styleUrls() {
        return {
            "$": ["bal-accordion-summary.css"]
        };
    }
    static get properties() {
        return {
            "trigger": {
                "type": "boolean",
                "mutable": false,
                "complexType": {
                    "original": "boolean",
                    "resolved": "boolean",
                    "references": {}
                },
                "required": false,
                "optional": false,
                "docs": {
                    "tags": [],
                    "text": "If `true` the whole summary component acts as a trigger and can be clicked"
                },
                "getter": false,
                "setter": false,
                "reflect": false,
                "attribute": "trigger",
                "defaultValue": "false"
            },
            "active": {
                "type": "boolean",
                "mutable": true,
                "complexType": {
                    "original": "boolean",
                    "resolved": "boolean",
                    "references": {}
                },
                "required": false,
                "optional": false,
                "docs": {
                    "tags": [{
                            "name": "internal",
                            "text": undefined
                        }],
                    "text": ""
                },
                "getter": false,
                "setter": false,
                "reflect": true,
                "attribute": "active",
                "defaultValue": "false"
            },
            "state": {
                "type": "number",
                "mutable": false,
                "complexType": {
                    "original": "AccordionState",
                    "resolved": "AccordionState.Collapsed | AccordionState.Collapsing | AccordionState.Expanded | AccordionState.Expanding",
                    "references": {
                        "AccordionState": {
                            "location": "import",
                            "path": "../../../interfaces",
                            "id": "src/interfaces.d.ts::AccordionState"
                        }
                    }
                },
                "required": false,
                "optional": false,
                "docs": {
                    "tags": [{
                            "name": "internal",
                            "text": undefined
                        }],
                    "text": ""
                },
                "getter": false,
                "setter": false,
                "reflect": false,
                "attribute": "state",
                "defaultValue": "AccordionState.Collapsed"
            }
        };
    }
    static get states() {
        return {
            "parentAccordionId": {}
        };
    }
    static get elementRef() { return "el"; }
}
__decorate([
    Logger('bal-accordion-summary'),
    __metadata("design:type", Function),
    __metadata("design:paramtypes", [Function]),
    __metadata("design:returntype", void 0)
], AccordionSummary.prototype, "createLogger", null);
let accordionSummaryIds = 0;
