/*!
 * KoliBri - The accessible HTML-Standard
 */
import { h, Host } from "@stencil/core";
import { KolCardWcTag } from "../../core/component-names";
import { setState, validateAlign, validateHasCloser, validateLabel, validateOpen } from "../../schema";
import clsx from "../../utils/clsx";
import { createUniqueId } from "../../utils/dev.utils";
import { dispatchDomEvent, KolEvent } from "../../utils/events";
import { handleCancelOverlay } from "../../utils/tooltip-open-tracking";
import { watchHeadingLevel } from "../heading/validation";
export class KolDrawer {
    constructor() {
        this.cardHeadingId = createUniqueId('drawer-heading');
        this.isModal = true;
        this._cardOn = { onClose: () => void this.close() };
        this.getWrapperRef = (el) => (this.dialogWrapperElement = el);
        this.getRef = (el) => {
            this.dialogElement = el;
            setTimeout(() => {
                void this.openOrCloseBasedOnState();
            });
        };
        this._hasCloser = false;
        this._level = 0;
        this.state = {
            _label: '',
            _open: false,
            _align: 'left',
        };
        this.handleCancelEvent = (event) => {
            var _a, _b;
            handleCancelOverlay(event);
            if (event.defaultPrevented)
                return;
            (_b = (_a = this.state._on) === null || _a === void 0 ? void 0 : _a.onCancel) === null || _b === void 0 ? void 0 : _b.call(_a, event);
            if (event.defaultPrevented)
                return;
            if (this.host && !dispatchDomEvent(this.host, KolEvent.cancel)) {
                event.preventDefault();
            }
        };
        this.handleClose = () => {
            void (async () => {
                await this.close();
                this.handleCloseDialog();
            })();
        };
        this.handleAnimationEnd = (e) => {
            var _a, _b;
            const animationEvent = e;
            if (animationEvent.animationName.includes('slideOut')) {
                (_b = (_a = this.dialogElement) === null || _a === void 0 ? void 0 : _a.close) === null || _b === void 0 ? void 0 : _b.call(_a);
            }
        };
    }
    async show(modal = false) {
        var _a, _b, _c, _d, _e, _f, _g;
        if ((_a = this.dialogElement) === null || _a === void 0 ? void 0 : _a.open) {
            return;
        }
        this.isModal = modal;
        this.state = Object.assign(Object.assign({}, this.state), { _open: true });
        if (modal) {
            (_c = (_b = this.dialogElement) === null || _b === void 0 ? void 0 : _b.showModal) === null || _c === void 0 ? void 0 : _c.call(_b);
        }
        else {
            (_e = (_d = this.dialogElement) === null || _d === void 0 ? void 0 : _d.show) === null || _e === void 0 ? void 0 : _e.call(_d);
        }
        (_g = (_f = this._on) === null || _f === void 0 ? void 0 : _f.onToggle) === null || _g === void 0 ? void 0 : _g.call(_f, true);
        if (this.host) {
            dispatchDomEvent(this.host, KolEvent.toggle);
        }
    }
    showModal() {
        return this.show(true);
    }
    open() {
        return this.show(false);
    }
    async close() {
        var _a, _b;
        this.state = Object.assign(Object.assign({}, this.state), { _open: false });
        const wrapper = this.dialogWrapperElement;
        if (!wrapper) {
            return;
        }
        if (window.getComputedStyle(wrapper).animationName === 'none') {
            (_b = (_a = this.dialogElement) === null || _a === void 0 ? void 0 : _a.close) === null || _b === void 0 ? void 0 : _b.call(_a);
        }
    }
    renderDialogContent() {
        const align = this.state._align;
        return (h(KolCardWcTag, { ref: this.getWrapperRef, class: clsx(`kol-drawer__wrapper`, `kol-drawer__wrapper--${align}`, {
                'kol-drawer__wrapper--open': this.state._open,
                'kol-drawer__wrapper--is-closing': this.state._open === false,
            }), _hasCloser: this.state._hasCloser, _headingId: this.cardHeadingId, _label: this.state._label, _level: this._level, _on: this._cardOn }, h("div", { class: "kol-drawer__content" }, h("slot", null))));
    }
    render() {
        return (h(Host, { key: '9ddd7432cb3f0a79b4020144378e951d76d6429b', class: "kol-drawer" }, h("dialog", { key: '4187cc6a21ca4471a30f1db9f860d5a141372157', "aria-labelledby": this.cardHeadingId, "aria-modal": this.isModal ? 'true' : 'false', class: "kol-drawer__dialog", onCancel: this.handleCancelEvent, ref: this.getRef }, this.renderDialogContent())));
    }
    validateLabel(value) {
        validateLabel(this, value, {
            required: true,
        });
    }
    validateAlign(value) {
        validateAlign(this, value);
    }
    validateHasCloser(value) {
        validateHasCloser(this, value);
    }
    validateLevel(value) {
        watchHeadingLevel(this, value);
    }
    validateOpen(value) {
        if (typeof value === 'boolean') {
            validateOpen(this, value);
            if (this.dialogElement) {
                void this.openOrCloseBasedOnState();
            }
        }
    }
    async openOrCloseBasedOnState() {
        if (this.state._open) {
            await this.show(this.isModal);
        }
        else {
            await this.close();
        }
    }
    validateOn(value) {
        if (typeof value === 'object' && value !== null) {
            const callbacks = {};
            if (typeof value.onCancel === 'function') {
                callbacks.onCancel = value.onCancel;
            }
            if (typeof value.onClose === 'function') {
                callbacks.onClose = value.onClose;
            }
            if (typeof value.onToggle === 'function') {
                callbacks.onToggle = value.onToggle;
            }
            setState(this, '_on', callbacks);
        }
    }
    handleCloseDialog() {
        var _a, _b, _c, _d;
        (_b = (_a = this._on) === null || _a === void 0 ? void 0 : _a.onClose) === null || _b === void 0 ? void 0 : _b.call(_a);
        (_d = (_c = this._on) === null || _c === void 0 ? void 0 : _c.onToggle) === null || _d === void 0 ? void 0 : _d.call(_c, false);
        if (this.host) {
            dispatchDomEvent(this.host, KolEvent.close);
            dispatchDomEvent(this.host, KolEvent.toggle);
        }
    }
    componentDidLoad() {
        var _a, _b;
        (_a = this.dialogElement) === null || _a === void 0 ? void 0 : _a.addEventListener('animationend', this.handleAnimationEnd);
        (_b = this.dialogElement) === null || _b === void 0 ? void 0 : _b.addEventListener('close', this.handleClose);
    }
    disconnectedCallback() {
        var _a, _b;
        (_a = this.dialogElement) === null || _a === void 0 ? void 0 : _a.removeEventListener('animationend', this.handleAnimationEnd);
        (_b = this.dialogElement) === null || _b === void 0 ? void 0 : _b.removeEventListener('close', this.handleClose);
    }
    componentWillLoad() {
        this.validateAlign(this._align);
        this.validateHasCloser(this._hasCloser);
        this.validateLabel(this._label);
        this.validateOpen(this._open);
        this.validateLevel(this._level);
        this.validateOn(this._on);
    }
    static get is() { return "kol-drawer"; }
    static get encapsulation() { return "shadow"; }
    static get originalStyleUrls() {
        return {
            "default": ["./style.scss"]
        };
    }
    static get styleUrls() {
        return {
            "default": ["style.css"]
        };
    }
    static get properties() {
        return {
            "_open": {
                "type": "boolean",
                "mutable": false,
                "complexType": {
                    "original": "OpenPropType",
                    "resolved": "boolean | undefined",
                    "references": {
                        "OpenPropType": {
                            "location": "import",
                            "path": "../../schema",
                            "id": "src/schema/index.ts::OpenPropType"
                        }
                    }
                },
                "required": false,
                "optional": true,
                "docs": {
                    "tags": [],
                    "text": "Opens/expands the element when truthy, closes/collapses when falsy."
                },
                "getter": false,
                "setter": false,
                "reflect": false,
                "attribute": "_open"
            },
            "_align": {
                "type": "string",
                "mutable": false,
                "complexType": {
                    "original": "AlignPropType",
                    "resolved": "\"bottom\" | \"left\" | \"right\" | \"top\" | undefined",
                    "references": {
                        "AlignPropType": {
                            "location": "import",
                            "path": "../../schema",
                            "id": "src/schema/index.ts::AlignPropType"
                        }
                    }
                },
                "required": false,
                "optional": true,
                "docs": {
                    "tags": [],
                    "text": "Defines the visual orientation of the component."
                },
                "getter": false,
                "setter": false,
                "reflect": false,
                "attribute": "_align"
            },
            "_hasCloser": {
                "type": "boolean",
                "mutable": false,
                "complexType": {
                    "original": "boolean",
                    "resolved": "boolean | undefined",
                    "references": {}
                },
                "required": false,
                "optional": true,
                "docs": {
                    "tags": [{
                            "name": "TODO",
                            "text": ": Change type back to `HasCloserPropType` after Stencil#4663 has been resolved."
                        }],
                    "text": "Defines whether the element can be closed."
                },
                "getter": false,
                "setter": false,
                "reflect": false,
                "attribute": "_has-closer",
                "defaultValue": "false"
            },
            "_label": {
                "type": "string",
                "mutable": false,
                "complexType": {
                    "original": "LabelPropType",
                    "resolved": "string",
                    "references": {
                        "LabelPropType": {
                            "location": "import",
                            "path": "../../schema",
                            "id": "src/schema/index.ts::LabelPropType"
                        }
                    }
                },
                "required": true,
                "optional": false,
                "docs": {
                    "tags": [],
                    "text": "Defines the visible or semantic label of the component (e.g. aria-label, label, headline, caption, summary, etc.)."
                },
                "getter": false,
                "setter": false,
                "reflect": false,
                "attribute": "_label"
            },
            "_level": {
                "type": "number",
                "mutable": false,
                "complexType": {
                    "original": "HeadingLevel",
                    "resolved": "0 | 1 | 2 | 3 | 4 | 5 | 6 | undefined",
                    "references": {
                        "HeadingLevel": {
                            "location": "import",
                            "path": "../../schema",
                            "id": "src/schema/index.ts::HeadingLevel"
                        }
                    }
                },
                "required": false,
                "optional": true,
                "docs": {
                    "tags": [],
                    "text": "Defines which H-level from 1-6 the heading has. 0 specifies no heading and is shown as bold text."
                },
                "getter": false,
                "setter": false,
                "reflect": false,
                "attribute": "_level",
                "defaultValue": "0"
            },
            "_on": {
                "type": "unknown",
                "mutable": false,
                "complexType": {
                    "original": "KoliBriModalEventCallbacks",
                    "resolved": "undefined | ({ onCancel?: ((event: Event) => void) | undefined; onClose?: (() => void) | undefined; onToggle?: ((open: boolean) => void) | undefined; })",
                    "references": {
                        "KoliBriModalEventCallbacks": {
                            "location": "import",
                            "path": "../../schema",
                            "id": "src/schema/index.ts::KoliBriModalEventCallbacks"
                        }
                    }
                },
                "required": false,
                "optional": true,
                "docs": {
                    "tags": [],
                    "text": "Specifies the EventCallback function to be called when the drawer is closing."
                },
                "getter": false,
                "setter": false
            }
        };
    }
    static get states() {
        return {
            "isModal": {},
            "state": {}
        };
    }
    static get methods() {
        return {
            "show": {
                "complexType": {
                    "signature": "(modal?: boolean) => Promise<void>",
                    "parameters": [{
                            "name": "modal",
                            "type": "boolean",
                            "docs": ""
                        }],
                    "references": {
                        "Promise": {
                            "location": "global",
                            "id": "global::Promise"
                        }
                    },
                    "return": "Promise<void>"
                },
                "docs": {
                    "text": "Opens the drawer. Pass true to open as a modal drawer.",
                    "tags": []
                }
            },
            "showModal": {
                "complexType": {
                    "signature": "() => Promise<void>",
                    "parameters": [],
                    "references": {
                        "Promise": {
                            "location": "global",
                            "id": "global::Promise"
                        }
                    },
                    "return": "Promise<void>"
                },
                "docs": {
                    "text": "Opens the drawer as a modal.",
                    "tags": []
                }
            },
            "open": {
                "complexType": {
                    "signature": "() => Promise<void>",
                    "parameters": [],
                    "references": {
                        "Promise": {
                            "location": "global",
                            "id": "global::Promise"
                        }
                    },
                    "return": "Promise<void>"
                },
                "docs": {
                    "text": "Opens the drawer.",
                    "tags": [{
                            "name": "deprecated",
                            "text": "Use show() or showModal() instead."
                        }]
                }
            },
            "close": {
                "complexType": {
                    "signature": "() => Promise<void>",
                    "parameters": [],
                    "references": {
                        "Promise": {
                            "location": "global",
                            "id": "global::Promise"
                        }
                    },
                    "return": "Promise<void>"
                },
                "docs": {
                    "text": "Closes the drawer.",
                    "tags": []
                }
            }
        };
    }
    static get elementRef() { return "host"; }
    static get watchers() {
        return [{
                "propName": "_label",
                "methodName": "validateLabel"
            }, {
                "propName": "_align",
                "methodName": "validateAlign"
            }, {
                "propName": "_hasCloser",
                "methodName": "validateHasCloser"
            }, {
                "propName": "_level",
                "methodName": "validateLevel"
            }, {
                "propName": "_open",
                "methodName": "validateOpen"
            }, {
                "propName": "_on",
                "methodName": "validateOn"
            }];
    }
}
//# sourceMappingURL=shadow.js.map
