import { Host, h } from "@stencil/core";
import { isSlotUsed, DEVICE_SIZES, getCurrentDeviceSize, checkResizeObserver, } from "../../utils/helpers";
/**
 * @slot breadcrumbs - Content will be rendered in the breadcrumb area, above the title and action areas.
 * @slot heading - Content will be rendered in the title area, in place of the heading.
 * @slot subheading - Content will be rendered in the title area, in place of the subheading.
 * @slot heading-adornment - Content will be rendered in the title area, adjacent to the heading.
 * @slot actions - Content will be rendered in the action area, adjacent to the title area.
 * @slot input - Content will be rendered in the input area, below the title area and adjacent to the action area.
 * @slot stepper - Content will be rendered in the navigation area, below the title and action areas. Note: stepper slot cannot be used when the tabs slot is being used.
 * @slot tabs - Content will be rendered in the navigation area, below the title and action areas. Note: the stepper slot cannot be used when the tabs slot is being used.
 */
export class PageHeader {
    constructor() {
        this.resizeObserver = null;
        this.areButtonsReversed = false;
        this.deviceSize = DEVICE_SIZES.XL;
        /**
         * The alignment of the page header.
         */
        this.aligned = "left";
        /**
         * If `true`, a border will be displayed along the bottom of the page header.
         */
        this.border = true;
        /**
         * If `true`, the reading pattern and tab order will change in the action area for viewport widths of above 576px and when actions have not wrapped.
         */
        this.reverseOrder = false;
        /**
         * The size of the page header component.
         */
        this.size = "medium";
        /**
         * If `true`, the page header will be sticky at all breakpoints.
         */
        this.sticky = false;
        /**
         *  If `true`, the page header will only be sticky for viewport widths of 992px and above.
         */
        this.stickyDesktopOnly = false;
        /**
         * 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";
        this.resizeObserverCallback = () => {
            if (this.reverseOrder && isSlotUsed(this.el, "actions")) {
                this.applyReverseOrder();
            }
        };
        this.runResizeObserver = () => {
            this.resizeObserver = new ResizeObserver(() => {
                this.resizeObserverCallback();
            });
            this.resizeObserver.observe(this.el);
        };
        this.applyReverseOrder = () => {
            var _a;
            const currSize = getCurrentDeviceSize();
            if (currSize !== this.deviceSize) {
                this.deviceSize = currSize;
            }
            const actionArea = (_a = this.el.shadowRoot) === null || _a === void 0 ? void 0 : _a.querySelector("div.action-area");
            const actionHeights = [];
            /* For each button in the actions slot, check the height of the button and push this number into the actionHeights array and then find the greatest number in this array (i.e. find the height of the tallest button) */
            for (let i = 0; i < this.actionContent.length; i++) {
                const action = this.actionContent[i];
                let actionHeight = action.offsetHeight;
                if (actionHeight === undefined) {
                    actionHeight = 0;
                }
                actionHeights.push(actionHeight);
            }
            const max = Math.max(...actionHeights);
            /* The smallest breakpoint displays buttons in the same order the user has slotted them in. When the device size is larger than the small breakpoint then the order of the button reverses due to best practice for reading patterns. If the device size is larger than the small breakpoint and the buttons have started to wrap then they follow the order the user has slotted them in. This logic is programmed based on the height of the action-area div that wraps the actions slot. If the height of the action-area div is taller than the height of the tallest button, then it is assumed that the taller height is caused by the buttons wrapping */
            let actionAreaHeight = actionArea.offsetHeight;
            if (actionAreaHeight === undefined) {
                actionAreaHeight = 0;
            }
            const appendActionContent = () => {
                this.actionContent = this.actionContent.reverse();
                this.actionContent.forEach((btn) => {
                    this.el.append(btn);
                });
                this.areButtonsReversed = !this.areButtonsReversed;
            };
            if ((this.deviceSize > DEVICE_SIZES.S &&
                actionAreaHeight <= max &&
                !this.areButtonsReversed) ||
                (((this.deviceSize > DEVICE_SIZES.S && actionAreaHeight > max) ||
                    this.deviceSize <= DEVICE_SIZES.S) &&
                    this.areButtonsReversed)) {
                appendActionContent();
            }
        };
    }
    disconnectedCallback() {
        if (this.resizeObserver !== null) {
            this.resizeObserver.disconnect();
        }
    }
    componentWillLoad() {
        this.actionContent = Array.from(this.el.querySelectorAll(`[slot="actions"]`));
    }
    componentDidLoad() {
        checkResizeObserver(this.runResizeObserver);
    }
    render() {
        var _a, _b;
        const { size, border, heading, subheading, aligned, sticky, stickyDesktopOnly, theme, } = this;
        const navAriaLabel = `${isSlotUsed(this.el, "heading")
            ? (_b = (_a = this.el.querySelector('[slot="heading"]')) === null || _a === void 0 ? void 0 : _a.textContent) !== null && _b !== void 0 ? _b : ""
            : heading !== null && heading !== void 0 ? heading : ""} page sections`;
        return (h(Host, { key: 'ceafa3fbf2b8f895578899be8b6261716d90b547', class: {
                ["ic-page-header-sticky"]: !!sticky,
                ["ic-page-header-sticky-desktop"]: !sticky && !!stickyDesktopOnly,
                [`ic-theme-${theme}`]: theme !== "inherit",
            }, "aria-label": this.el.ariaLabel || "page header" }, h("header", { key: '74fd64da431d176828c36fce3700f6823099a46f', class: {
                ["border-bottom"]: !!border,
                ["tabs"]: isSlotUsed(this.el, "tabs"),
            }, role: "presentation" }, h("ic-section-container", { key: '71b33b7159b25af11e3b2c3d85e3a508f5cd9685', aligned: aligned, fullHeight: isSlotUsed(this.el, "tabs") }, isSlotUsed(this.el, "breadcrumbs") && (h("div", { key: '34ffcb850ffdc99bd22635d9d17d0d1173c7a5ab', class: "breadcrumb-area" }, h("slot", { key: '836354d69859b1e475a835e5f8553e2f58d9a109', name: "breadcrumbs" }))), h("div", { key: '0a00aca54f9bef3158e5309f8e8252fe31885181', class: "main-content" }, h("div", { key: '47e7b5d1848e5068a957e4c57998f4a25c81f0f2', class: "title-area" }, h("div", { key: '1053e02597294a806e0270113214cdccf3ed5c49', class: "header-content" }, h("slot", { key: '678c34e2382d8751f3ca7cfdb6f83122745b36fd', name: "heading" }, h("ic-typography", { key: '36024ad870d7cf7f0be284a461d9b0923f1079bf', variant: size === "small" ? "h4" : "h2", class: "heading" }, h("h2", { key: 'd0e55b2328d2abbd5a1e0ed284a3f140ef8457dd' }, heading))), h("slot", { key: '3a7513ccc6ef78abd15fefbda7ac63b202163441', name: "heading-adornment" })), h("div", { key: 'fdb61ca7d9f05a1dfd258a8f7697258118375888', class: {
                ["subheading-content"]: !!subheading || isSlotUsed(this.el, "subheading"),
                ["small"]: size === "small",
            } }, h("slot", { key: 'ebc0a0dd4bd7114bc2c392b51b0d182b7aa6a429', name: "subheading" }, h("ic-typography", { key: '649da015dc08acecdac04e1c6102ce0717dca647', variant: "body" }, subheading)))), isSlotUsed(this.el, "actions") && (h("div", { key: '47d5820db4cfbccad118d3f8a7df876b7ee53b2d', class: "action-area" }, h("slot", { key: 'b19fef794de8ac0024287dc70e4bc1fc548ec970', name: "actions" }))), isSlotUsed(this.el, "input") && (h("div", { key: '8626ca99df441a1e1d0f721994e9212b28aebd8c', class: "input-area" }, h("slot", { key: '6d65e1e7b9fba98cc426e5934e054fa739074abd', name: "input" })))), (isSlotUsed(this.el, "stepper") ||
            isSlotUsed(this.el, "tabs")) && (h("div", { key: '362e4d4830c6ebb5ebdb588ce308f53c7af2d747', class: "navigation-area" }, isSlotUsed(this.el, "stepper") &&
            !isSlotUsed(this.el, "tabs") && h("slot", { key: 'edec3599df03352fb98cfdc550c16eb66520df60', name: "stepper" }), isSlotUsed(this.el, "tabs") && (h("nav", { key: 'a77c78ab6ec529b5ec460882e41c0ca490cf16c4', "aria-label": navAriaLabel }, h("ic-horizontal-scroll", { key: 'dc2baad92838b20c5940cb60e6c7c980e6dea918' }, h("ul", { key: '3d48bcfaaaabbc311b5f51b21cbf490fb2bbe7dd', class: "tabs-slot" }, h("slot", { key: '18534f16226ec1b3e13446a13043e568af9c6227', name: "tabs" })))))))))));
    }
    static get is() { return "ic-page-header"; }
    static get encapsulation() { return "shadow"; }
    static get originalStyleUrls() {
        return {
            "$": ["ic-page-header.css"]
        };
    }
    static get styleUrls() {
        return {
            "$": ["ic-page-header.css"]
        };
    }
    static get properties() {
        return {
            "aligned": {
                "type": "string",
                "mutable": false,
                "complexType": {
                    "original": "IcAlignment",
                    "resolved": "\"center\" | \"full-width\" | \"left\" | undefined",
                    "references": {
                        "IcAlignment": {
                            "location": "import",
                            "path": "../../utils/types",
                            "id": "src/utils/types.ts::IcAlignment"
                        }
                    }
                },
                "required": false,
                "optional": true,
                "docs": {
                    "tags": [],
                    "text": "The alignment of the page header."
                },
                "getter": false,
                "setter": false,
                "attribute": "aligned",
                "reflect": false,
                "defaultValue": "\"left\""
            },
            "border": {
                "type": "boolean",
                "mutable": false,
                "complexType": {
                    "original": "boolean",
                    "resolved": "boolean | undefined",
                    "references": {}
                },
                "required": false,
                "optional": true,
                "docs": {
                    "tags": [],
                    "text": "If `true`, a border will be displayed along the bottom of the page header."
                },
                "getter": false,
                "setter": false,
                "attribute": "border",
                "reflect": false,
                "defaultValue": "true"
            },
            "heading": {
                "type": "string",
                "mutable": false,
                "complexType": {
                    "original": "string",
                    "resolved": "string | undefined",
                    "references": {}
                },
                "required": false,
                "optional": true,
                "docs": {
                    "tags": [],
                    "text": "The title to render on the page header."
                },
                "getter": false,
                "setter": false,
                "attribute": "heading",
                "reflect": false
            },
            "reverseOrder": {
                "type": "boolean",
                "mutable": false,
                "complexType": {
                    "original": "boolean",
                    "resolved": "boolean | undefined",
                    "references": {}
                },
                "required": false,
                "optional": true,
                "docs": {
                    "tags": [],
                    "text": "If `true`, the reading pattern and tab order will change in the action area for viewport widths of above 576px and when actions have not wrapped."
                },
                "getter": false,
                "setter": false,
                "attribute": "reverse-order",
                "reflect": false,
                "defaultValue": "false"
            },
            "size": {
                "type": "string",
                "mutable": false,
                "complexType": {
                    "original": "IcSizesNoLarge",
                    "resolved": "\"medium\" | \"small\" | undefined",
                    "references": {
                        "IcSizesNoLarge": {
                            "location": "import",
                            "path": "../../utils/types",
                            "id": "src/utils/types.ts::IcSizesNoLarge"
                        }
                    }
                },
                "required": false,
                "optional": true,
                "docs": {
                    "tags": [],
                    "text": "The size of the page header component."
                },
                "getter": false,
                "setter": false,
                "attribute": "size",
                "reflect": false,
                "defaultValue": "\"medium\""
            },
            "sticky": {
                "type": "boolean",
                "mutable": false,
                "complexType": {
                    "original": "boolean",
                    "resolved": "boolean | undefined",
                    "references": {}
                },
                "required": false,
                "optional": true,
                "docs": {
                    "tags": [],
                    "text": "If `true`, the page header will be sticky at all breakpoints."
                },
                "getter": false,
                "setter": false,
                "attribute": "sticky",
                "reflect": false,
                "defaultValue": "false"
            },
            "stickyDesktopOnly": {
                "type": "boolean",
                "mutable": false,
                "complexType": {
                    "original": "boolean",
                    "resolved": "boolean | undefined",
                    "references": {}
                },
                "required": false,
                "optional": true,
                "docs": {
                    "tags": [],
                    "text": "If `true`, the page header will only be sticky for viewport widths of 992px and above."
                },
                "getter": false,
                "setter": false,
                "attribute": "sticky-desktop-only",
                "reflect": false,
                "defaultValue": "false"
            },
            "subheading": {
                "type": "string",
                "mutable": false,
                "complexType": {
                    "original": "string",
                    "resolved": "string | undefined",
                    "references": {}
                },
                "required": false,
                "optional": true,
                "docs": {
                    "tags": [],
                    "text": "The subtitle to render on the page header."
                },
                "getter": false,
                "setter": false,
                "attribute": "subheading",
                "reflect": false
            },
            "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\""
            }
        };
    }
    static get states() {
        return {
            "actionContent": {},
            "areButtonsReversed": {},
            "deviceSize": {}
        };
    }
    static get elementRef() { return "el"; }
}
//# sourceMappingURL=ic-page-header.js.map
