import { h, Host, } from "@stencil/core";
import { isSlotUsed } from "../../utils/helpers";
let accordionGroupIds = 0;
/**
 * @slot label - Content is placed as the accordion group title.
 */
export class AccordionGroup {
    constructor() {
        this.accordionGroupId = `ic-accordion-group-${accordionGroupIds++}`;
        /**
         * The accessible button label to provide more context to the 'See all/Hide all' button for screen reader users.
         */
        this.accessibleButtonLabel = "accordions";
        /**
         * Sets the theme color to the dark or light theme color. "inherit" will set the color based on the system settings or ic-theme component.
         */
        this.theme = "inherit";
        /**
         * If `true`, the accordion will load in an expanded state.
         */
        this.expanded = false;
        /**
         * The header for the accordion group.
         */
        this.label = "";
        /**
         * If `true`, only one accordion will open at a time.
         */
        this.singleExpansion = false;
        /**
         * The size of the accordion.
         */
        this.size = "medium";
        this.handleExpanded = () => {
            if (this.areAllAccordionsOpen) {
                this.expanded = false;
                this.accordions.forEach((acc) => {
                    acc.expanded = this.expanded;
                });
            }
            else {
                this.expanded = true;
                this.accordions.forEach((acc) => {
                    acc.expanded = this.expanded;
                });
            }
            this.setExpandedToAreAllAccordionsOpen();
        };
        this.linkAccordions = () => {
            this.accordions.forEach((accordion) => {
                accordion.setAttribute("context-id", this.accordionGroupId);
            });
        };
        this.setExpandedToAreAllAccordionsOpen = () => {
            this.areAllAccordionsOpen = this.accordions.every((accordion) => !!accordion.expanded);
        };
        this.accordionOpenBtnText = () => {
            return !this.areAllAccordionsOpen ? "See all" : "Hide all";
        };
    }
    watchThemeHandler() {
        this.accordions.forEach((acc) => {
            acc.theme = this.theme;
        });
    }
    watchExpandedHandler() {
        this.handleExpanded();
    }
    componentDidLoad() {
        const accordionDirectChildren = this.el.children;
        this.accordions = Array.from(accordionDirectChildren).filter((child) => child.tagName === "IC-ACCORDION");
        this.linkAccordions();
        this.accordions.forEach((acc) => {
            acc.theme = this.theme;
        });
        this.accordions.forEach((acc) => {
            acc.size = this.size;
        });
        if (this.expanded) {
            this.accordions.forEach((acc) => {
                acc.expanded = true;
            });
            this.setExpandedToAreAllAccordionsOpen();
        }
        else {
            this.setExpandedToAreAllAccordionsOpen();
            this.expanded = this.areAllAccordionsOpen;
        }
    }
    handleAccordionClicked(event) {
        if (!this.singleExpansion) {
            // 'See all' should be visible until all accordions are open, then 'Hide all' should be visible
            this.setExpandedToAreAllAccordionsOpen();
        }
        else {
            this.accordions.forEach((acc) => {
                if (acc.expanded && event.detail.id !== acc.id) {
                    acc.expanded = false;
                }
            });
        }
    }
    /**
     * Sets the focus on first focusable element in the accordion group. If the "See/Hide all" button is present, it will be focused.
     * Otherwise, the first accordion will be focused.
     */
    async setFocus() {
        const focusEl = this.singleExpansion
            ? this.accordions[0]
            : this.allButtonEl;
        focusEl === null || focusEl === void 0 ? void 0 : focusEl.setFocus();
    }
    render() {
        const { size, label, singleExpansion, accessibleButtonLabel, theme } = this;
        return (h(Host, { key: 'ec057d7f6e128e8429ebd9e14497a86d1eaf1abe', "context-id": this.accordionGroupId, class: {
                [`ic-accordion-group-${size}`]: true,
                ["ic-accordion-group"]: true,
                [`ic-theme-${theme}`]: theme !== "inherit",
            } }, h("div", { key: '86acdf79730dc3d2dcc95a5928e1a1bebf83412f', class: "label-container" }, h("ic-typography", { key: '5f2b615ebe87304b410b896d5b102da42ff5bcd6', variant: "h4" }, h("h3", { key: '9b33f5f102e27e4268b0edd894d8d725fd8cc5a4' }, isSlotUsed(this.el, "label") ? h("slot", { name: "label" }) : label)), !singleExpansion && (h("ic-button", { key: '1b8d8332cd10fed2eea6959f02841cb4b1586902', ref: (el) => (this.allButtonEl = el), onClick: this.handleExpanded, variant: "tertiary", "aria-label": `${this.accordionOpenBtnText()} ${accessibleButtonLabel}` }, this.accordionOpenBtnText()))), h("slot", { key: 'b93775721fd0cc7560115c6bb62824db59b2ea27' })));
    }
    static get is() { return "ic-accordion-group"; }
    static get encapsulation() { return "shadow"; }
    static get originalStyleUrls() {
        return {
            "$": ["ic-accordion-group.css"]
        };
    }
    static get styleUrls() {
        return {
            "$": ["ic-accordion-group.css"]
        };
    }
    static get properties() {
        return {
            "accessibleButtonLabel": {
                "type": "string",
                "mutable": false,
                "complexType": {
                    "original": "string",
                    "resolved": "string | undefined",
                    "references": {}
                },
                "required": false,
                "optional": true,
                "docs": {
                    "tags": [],
                    "text": "The accessible button label to provide more context to the 'See all/Hide all' button for screen reader users."
                },
                "getter": false,
                "setter": false,
                "attribute": "accessible-button-label",
                "reflect": false,
                "defaultValue": "\"accordions\""
            },
            "theme": {
                "type": "string",
                "mutable": false,
                "complexType": {
                    "original": "IcThemeMode",
                    "resolved": "\"dark\" | \"inherit\" | \"light\" | undefined",
                    "references": {
                        "IcThemeMode": {
                            "location": "import",
                            "path": "../../utils/types",
                            "id": "src/utils/types.ts::IcThemeMode"
                        }
                    }
                },
                "required": false,
                "optional": true,
                "docs": {
                    "tags": [],
                    "text": "Sets the theme color to the dark or light theme color. \"inherit\" will set the color based on the system settings or ic-theme component."
                },
                "getter": false,
                "setter": false,
                "attribute": "theme",
                "reflect": false,
                "defaultValue": "\"inherit\""
            },
            "expanded": {
                "type": "boolean",
                "mutable": true,
                "complexType": {
                    "original": "boolean",
                    "resolved": "boolean | undefined",
                    "references": {}
                },
                "required": false,
                "optional": true,
                "docs": {
                    "tags": [],
                    "text": "If `true`, the accordion will load in an expanded state."
                },
                "getter": false,
                "setter": false,
                "attribute": "expanded",
                "reflect": false,
                "defaultValue": "false"
            },
            "label": {
                "type": "string",
                "mutable": false,
                "complexType": {
                    "original": "string",
                    "resolved": "string | undefined",
                    "references": {}
                },
                "required": false,
                "optional": true,
                "docs": {
                    "tags": [],
                    "text": "The header for the accordion group."
                },
                "getter": false,
                "setter": false,
                "attribute": "label",
                "reflect": false,
                "defaultValue": "\"\""
            },
            "singleExpansion": {
                "type": "boolean",
                "mutable": false,
                "complexType": {
                    "original": "boolean",
                    "resolved": "boolean | undefined",
                    "references": {}
                },
                "required": false,
                "optional": true,
                "docs": {
                    "tags": [],
                    "text": "If `true`, only one accordion will open at a time."
                },
                "getter": false,
                "setter": false,
                "attribute": "single-expansion",
                "reflect": false,
                "defaultValue": "false"
            },
            "size": {
                "type": "string",
                "mutable": false,
                "complexType": {
                    "original": "IcSizes",
                    "resolved": "\"large\" | \"medium\" | \"small\" | undefined",
                    "references": {
                        "IcSizes": {
                            "location": "import",
                            "path": "../../utils/types",
                            "id": "src/utils/types.ts::IcSizes"
                        }
                    }
                },
                "required": false,
                "optional": true,
                "docs": {
                    "tags": [],
                    "text": "The size of the accordion."
                },
                "getter": false,
                "setter": false,
                "attribute": "size",
                "reflect": false,
                "defaultValue": "\"medium\""
            }
        };
    }
    static get states() {
        return {
            "accordions": {},
            "areAllAccordionsOpen": {}
        };
    }
    static get methods() {
        return {
            "setFocus": {
                "complexType": {
                    "signature": "() => Promise<void>",
                    "parameters": [],
                    "references": {
                        "Promise": {
                            "location": "global",
                            "id": "global::Promise"
                        }
                    },
                    "return": "Promise<void>"
                },
                "docs": {
                    "text": "Sets the focus on first focusable element in the accordion group. If the \"See/Hide all\" button is present, it will be focused.\nOtherwise, the first accordion will be focused.",
                    "tags": []
                }
            }
        };
    }
    static get elementRef() { return "el"; }
    static get watchers() {
        return [{
                "propName": "theme",
                "methodName": "watchThemeHandler"
            }, {
                "propName": "expanded",
                "methodName": "watchExpandedHandler"
            }];
    }
    static get listeners() {
        return [{
                "name": "accordionClicked",
                "method": "handleAccordionClicked",
                "target": undefined,
                "capture": false,
                "passive": false
            }];
    }
}
//# sourceMappingURL=ic-accordion-group.js.map
