import { h, Host, } from "@stencil/core";
import { sanitizeSvg } from "../../../utils/svg";
import { inheritAttributes } from "../../../utils/attributes";
import { BEM } from "../../../utils/bem";
import { rOnLoad, waitAfterFramePaint } from "../../../utils/helpers";
import { toKebabCase } from "../../../utils/string";
export class CarouselItem {
    constructor() {
        this.imageInheritAttributes = {};
        this.isOnLoadEventDone = false;
        this.containerId = '';
        this.svgContent = '';
        /**
         * Label of the slide which will be used for pagination tabs
         */
        this.label = '';
        /**
         * Svg content.
         */
        this.svg = '';
        /**
         * @deprecated
         * Defines the role of the carousel.
         */
        this.htmlRole = 'listitem';
        /**
         * The type of button.
         */
        this.elementType = 'button';
        /**
         * The name of the button, which is submitted with the form data.
         */
        this.name = undefined;
        /**
         * The value of the button, which is submitted with the form data.
         */
        this.value = undefined;
        /**
         * Specifies where to display the linked URL.
         * Only applies when an `href` is provided.
         */
        this.target = '_self';
        this.generateSvgContent = () => {
            if (this.svg !== undefined && this.svg.length > 0) {
                this.svgContent = sanitizeSvg(this.svg);
            }
        };
        this.onClick = (ev) => {
            if (this.href !== undefined) {
                this.balNavigate.emit(ev);
            }
        };
        this.onFocus = () => {
            this.balFocus.emit();
        };
        this.onBlur = () => {
            this.balBlur.emit();
        };
    }
    svgChanged() {
        this.generateSvgContent();
    }
    /**
     * LIFECYCLE
     * ------------------------------------------------------
     */
    connectedCallback() {
        this.generateSvgContent();
    }
    componentDidLoad() {
        rOnLoad(() => {
            this.isOnLoadEventDone = true;
        });
    }
    componentWillLoad() {
        this.imageInheritAttributes = inheritAttributes(this.el, ['alt']);
        this.getContainerId();
    }
    async setFocus() {
        await waitAfterFramePaint();
        if (this.buttonEl) {
            this.buttonEl.focus();
        }
    }
    async getContainerId() {
        const parentEl = this.el.closest('bal-carousel');
        this.containerId = await parentEl.getContainerId();
    }
    render() {
        const block = BEM.block('carousel');
        const itemEl = block.element('item');
        const isProduct = !!this.color && !!this.label;
        const parentEl = this.el.closest('bal-carousel');
        const role = parentEl && parentEl.controls === 'tabs' ? 'tabpanel' : 'listitem';
        const id = `${this.containerId}-${toKebabCase(this.label)}`;
        if (!isProduct) {
            return (h(Host, { id: id, role: role, class: Object.assign({}, itemEl.class()), "aria-label": this.label }, this.isOnLoadEventDone && this.src !== undefined ? (h("img", Object.assign({ draggable: false, onDragStart: () => false, src: this.src }, this.imageInheritAttributes))) : (''), h("slot", null)));
        }
        const button = itemEl.element('button');
        const image = button.element('image');
        const label = button.element('label');
        const { elementType, download, href, rel, target, name, value } = this;
        const TagType = this.href === undefined ? 'button' : 'a';
        const attrs = TagType === 'button'
            ? { type: elementType, name, value }
            : {
                download,
                href,
                rel,
                target,
            };
        return (h(Host, { id: id, role: role, "aria-label": this.label, class: Object.assign({}, itemEl.class()) }, h(TagType, Object.assign({}, attrs, { class: Object.assign(Object.assign({}, button.class()), button.modifier(`color-${this.color}`).class()), part: "native", onFocus: this.onFocus, onBlur: this.onBlur, onClick: this.onClick, ref: el => (this.buttonEl = el) }), this.isOnLoadEventDone && this.src !== undefined ? (h("img", Object.assign({ class: Object.assign({}, image.class()), draggable: false, onDragStart: () => false, "aria-hidden": "true", src: this.src }, this.imageInheritAttributes))) : this.isOnLoadEventDone && this.svgContent !== undefined ? (h("div", { class: Object.assign({}, image.class()), innerHTML: this.svgContent })) : (''), this.label !== undefined ? h("span", { class: Object.assign({}, label.class()) }, this.label) : '', h("slot", null))));
    }
    static get is() { return "bal-carousel-item"; }
    static get properties() {
        return {
            "src": {
                "type": "string",
                "mutable": false,
                "complexType": {
                    "original": "string",
                    "resolved": "string",
                    "references": {}
                },
                "required": false,
                "optional": true,
                "docs": {
                    "tags": [],
                    "text": "Src path to the image"
                },
                "getter": false,
                "setter": false,
                "reflect": true,
                "attribute": "src"
            },
            "label": {
                "type": "string",
                "mutable": false,
                "complexType": {
                    "original": "string",
                    "resolved": "string",
                    "references": {}
                },
                "required": false,
                "optional": false,
                "docs": {
                    "tags": [],
                    "text": "Label of the slide which will be used for pagination tabs"
                },
                "getter": false,
                "setter": false,
                "reflect": true,
                "attribute": "label",
                "defaultValue": "''"
            },
            "svg": {
                "type": "string",
                "mutable": false,
                "complexType": {
                    "original": "string",
                    "resolved": "string",
                    "references": {}
                },
                "required": false,
                "optional": false,
                "docs": {
                    "tags": [],
                    "text": "Svg content."
                },
                "getter": false,
                "setter": false,
                "reflect": false,
                "attribute": "svg",
                "defaultValue": "''"
            },
            "htmlRole": {
                "type": "string",
                "mutable": false,
                "complexType": {
                    "original": "'tab' | 'listitem' | ''",
                    "resolved": "\"\" | \"listitem\" | \"tab\"",
                    "references": {}
                },
                "required": false,
                "optional": false,
                "docs": {
                    "tags": [{
                            "name": "deprecated",
                            "text": "Defines the role of the carousel."
                        }],
                    "text": ""
                },
                "getter": false,
                "setter": false,
                "reflect": false,
                "attribute": "html-role",
                "defaultValue": "'listitem'"
            },
            "elementType": {
                "type": "string",
                "mutable": false,
                "complexType": {
                    "original": "BalProps.BalButtonElementType",
                    "resolved": "\"button\" | \"reset\" | \"submit\"",
                    "references": {
                        "BalProps": {
                            "location": "global",
                            "id": "global::BalProps"
                        }
                    }
                },
                "required": false,
                "optional": false,
                "docs": {
                    "tags": [],
                    "text": "The type of button."
                },
                "getter": false,
                "setter": false,
                "reflect": false,
                "attribute": "element-type",
                "defaultValue": "'button'"
            },
            "name": {
                "type": "string",
                "mutable": false,
                "complexType": {
                    "original": "string",
                    "resolved": "string",
                    "references": {}
                },
                "required": false,
                "optional": true,
                "docs": {
                    "tags": [],
                    "text": "The name of the button, which is submitted with the form data."
                },
                "getter": false,
                "setter": false,
                "reflect": true,
                "attribute": "name",
                "defaultValue": "undefined"
            },
            "value": {
                "type": "any",
                "mutable": false,
                "complexType": {
                    "original": "string | number",
                    "resolved": "number | string",
                    "references": {}
                },
                "required": false,
                "optional": true,
                "docs": {
                    "tags": [],
                    "text": "The value of the button, which is submitted with the form data."
                },
                "getter": false,
                "setter": false,
                "reflect": true,
                "attribute": "value",
                "defaultValue": "undefined"
            },
            "href": {
                "type": "string",
                "mutable": false,
                "complexType": {
                    "original": "string",
                    "resolved": "string",
                    "references": {}
                },
                "required": false,
                "optional": true,
                "docs": {
                    "tags": [],
                    "text": "Specifies the URL of the page the link goes to"
                },
                "getter": false,
                "setter": false,
                "reflect": false,
                "attribute": "href"
            },
            "target": {
                "type": "string",
                "mutable": false,
                "complexType": {
                    "original": "BalProps.BalButtonTarget",
                    "resolved": "\" _parent\" | \"_blank\" | \"_self\" | \"_top\"",
                    "references": {
                        "BalProps": {
                            "location": "global",
                            "id": "global::BalProps"
                        }
                    }
                },
                "required": false,
                "optional": false,
                "docs": {
                    "tags": [],
                    "text": "Specifies where to display the linked URL.\nOnly applies when an `href` is provided."
                },
                "getter": false,
                "setter": false,
                "reflect": false,
                "attribute": "target",
                "defaultValue": "'_self'"
            },
            "rel": {
                "type": "string",
                "mutable": false,
                "complexType": {
                    "original": "string",
                    "resolved": "string",
                    "references": {}
                },
                "required": false,
                "optional": true,
                "docs": {
                    "tags": [],
                    "text": "Specifies the relationship of the target object to the link object.\nThe value is a space-separated list of [link types](https://developer.mozilla.org/en-US/docs/Web/HTML/Link_types)."
                },
                "getter": false,
                "setter": false,
                "reflect": false,
                "attribute": "rel"
            },
            "download": {
                "type": "string",
                "mutable": false,
                "complexType": {
                    "original": "string",
                    "resolved": "string",
                    "references": {}
                },
                "required": false,
                "optional": true,
                "docs": {
                    "tags": [],
                    "text": "This attribute instructs browsers to download a URL instead of navigating to\nit, so the user will be prompted to save it as a local file. If the attribute\nhas a value, it is used as the pre-filled file name in the Save prompt\n(the user can still change the file name if they want)."
                },
                "getter": false,
                "setter": false,
                "reflect": false,
                "attribute": "download"
            },
            "color": {
                "type": "string",
                "mutable": false,
                "complexType": {
                    "original": "BalProps.BalCarouselItemColor",
                    "resolved": "\"green\" | \"purple\" | \"red\" | \"white\" | \"yellow\"",
                    "references": {
                        "BalProps": {
                            "location": "global",
                            "id": "global::BalProps"
                        }
                    }
                },
                "required": false,
                "optional": true,
                "docs": {
                    "tags": [],
                    "text": "Color of the background"
                },
                "getter": false,
                "setter": false,
                "reflect": false,
                "attribute": "color"
            }
        };
    }
    static get states() {
        return {
            "isOnLoadEventDone": {},
            "containerId": {},
            "svgContent": {}
        };
    }
    static get events() {
        return [{
                "method": "balNavigate",
                "name": "balNavigate",
                "bubbles": true,
                "cancelable": true,
                "composed": true,
                "docs": {
                    "tags": [],
                    "text": "Emitted when the link element has clicked."
                },
                "complexType": {
                    "original": "BalEvents.BalCarouselItemNavigateDetail",
                    "resolved": "MouseEvent",
                    "references": {
                        "BalEvents": {
                            "location": "global",
                            "id": "global::BalEvents"
                        }
                    }
                }
            }, {
                "method": "balFocus",
                "name": "balFocus",
                "bubbles": true,
                "cancelable": true,
                "composed": true,
                "docs": {
                    "tags": [],
                    "text": "Emitted when the button has focus."
                },
                "complexType": {
                    "original": "BalEvents.BalCarouselItemFocusDetail",
                    "resolved": "void",
                    "references": {
                        "BalEvents": {
                            "location": "global",
                            "id": "global::BalEvents"
                        }
                    }
                }
            }, {
                "method": "balBlur",
                "name": "balBlur",
                "bubbles": true,
                "cancelable": true,
                "composed": true,
                "docs": {
                    "tags": [],
                    "text": "Emitted when the button loses focus."
                },
                "complexType": {
                    "original": "BalEvents.BalCarouselItemBlurDetail",
                    "resolved": "void",
                    "references": {
                        "BalEvents": {
                            "location": "global",
                            "id": "global::BalEvents"
                        }
                    }
                }
            }];
    }
    static get methods() {
        return {
            "setFocus": {
                "complexType": {
                    "signature": "() => Promise<void>",
                    "parameters": [],
                    "references": {
                        "Promise": {
                            "location": "global",
                            "id": "global::Promise"
                        }
                    },
                    "return": "Promise<void>"
                },
                "docs": {
                    "text": "",
                    "tags": []
                }
            }
        };
    }
    static get elementRef() { return "el"; }
    static get watchers() {
        return [{
                "propName": "svg",
                "methodName": "svgChanged"
            }];
    }
}
