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: '17bc798fc01d1a70de9fed3da746d8e39a88e96c', 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: '1dadcdce3f21efe0f196171330cb5774be34075e', class: {
                ["border-bottom"]: !!border,
                ["tabs"]: isSlotUsed(this.el, "tabs"),
            }, role: "presentation" }, h("ic-section-container", { key: '9f0855c0310d29f5381c12665aad41ee1656ccdb', aligned: aligned, fullHeight: isSlotUsed(this.el, "tabs") }, isSlotUsed(this.el, "breadcrumbs") && (h("div", { key: '3b1fe9729e1d62e72a432fd039db40c1c5da22da', class: "breadcrumb-area" }, h("slot", { key: 'e5055a9f7def4018e2046dae4687ae1945235bf7', name: "breadcrumbs" }))), h("div", { key: '70acf27823b39d41d9c2f3dbe3e89d71096e7daf', class: "main-content" }, h("div", { key: 'b924bba3df8954b9f314c1eff6ffe4443d0e53f1', class: "title-area" }, h("div", { key: '5a60bb74557d37b33be8312d177e75ce77210998', class: "header-content" }, h("slot", { key: 'e4c7c70b13eac0944bba1a1f2ddd320f3e1c2659', name: "heading" }, h("ic-typography", { key: 'eb98510ca2b55a81c1b91bbabc4bfe7d5e28bdde', variant: size === "small" ? "h4" : "h2", class: "heading" }, h("h2", { key: '9bc94aa414b9514f0e349faa521804682f22f85b' }, heading))), h("slot", { key: '47b147656f0fe7436d233bb669c4462faab353db', name: "heading-adornment" })), h("div", { key: '1456f12fba38ec4cd05706ccb92d72adf12ae623', class: {
                ["subheading-content"]: !!subheading || isSlotUsed(this.el, "subheading"),
                ["small"]: size === "small",
            } }, h("slot", { key: 'a82707ce4abf37e4e8a965a39a5cc6e50d634647', name: "subheading" }, h("ic-typography", { key: '9f77da962becb04b244f3df0dceb7dad9dc122d8', variant: "body" }, subheading)))), isSlotUsed(this.el, "actions") && (h("div", { key: '792df65e46b45a50514a1967866dd72f6f72e683', class: "action-area" }, h("slot", { key: '0cd5fd825254df4815fe2d3e788946de88cbaae1', name: "actions" }))), isSlotUsed(this.el, "input") && (h("div", { key: 'e030cb3067c544b3d3496146e25d511a30ca97b2', class: "input-area" }, h("slot", { key: '8c9f722a205eded36526a4bc926e7a92c43626af', name: "input" })))), (isSlotUsed(this.el, "stepper") ||
            isSlotUsed(this.el, "tabs")) && (h("div", { key: 'deeb8c5520db91c494215d1adf90bebcc96fbbb5', class: "navigation-area" }, isSlotUsed(this.el, "stepper") &&
            !isSlotUsed(this.el, "tabs") && h("slot", { key: 'd9201c158069b054c3903ea4c0647d96de9d0930', name: "stepper" }), isSlotUsed(this.el, "tabs") && (h("nav", { key: 'a0cdbb20f507dd783536b0a60ae7fa2d391be85f', "aria-label": navAriaLabel }, h("ic-horizontal-scroll", { key: 'f3c15e0d61db5990108d86c5540d4f69b9477a9e' }, h("ul", { key: '6e7aded64c14703b5b4f141fa11f0f9eac247bce', class: "tabs-slot" }, h("slot", { key: 'eee6cfc3a09856108574b715163cc645f9eae627', 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
