import { Host, h } from "@stencil/core";
import { getInputHelperTextID, onComponentRequiredPropUndefined, } from "../../utils/helpers";
/**
 * @slot label - Content is placed as the label text.
 */
export class InputLabel {
    constructor() {
        /**
         * If `true`, the disabled state will be set.
         */
        this.disabled = false;
        /**
         * The helper text that will be displayed.
         */
        this.helperText = "";
        /**
         * The label will be visually hidden.
         */
        this.hideLabel = false;
        /**
         * If `true`, the readonly state will be set.
         */
        this.readonly = false;
        /**
         * If `true`, the input label will require a value.
         */
        this.required = false;
        /**
         * The status of the label - e.g. 'error'.
         */
        this.status = "";
        /**
         * @internal If `true`, wraps label text in label tag
         */
        this.useLabelTag = true;
        this.isSlotUsed = (slot) => {
            const assignedEls = slot === null || slot === void 0 ? void 0 : slot.assignedElements();
            if (assignedEls && assignedEls.length) {
                for (const el of assignedEls) {
                    if (el.tagName === "SLOT") {
                        // Recursion needed for when slot is forwarded multiple times - through child components
                        // (e.g. in date picker)
                        if (this.isSlotUsed(el)) {
                            return true;
                        }
                    }
                    else {
                        // Found an assigned element which is not a nested <slot>
                        return true;
                    }
                }
            }
            return false;
        };
    }
    componentDidLoad() {
        onComponentRequiredPropUndefined([{ prop: this.label, propName: "label" }], "Input Label");
    }
    render() {
        const { disabled, readonly, label, required, helperText, status, hideLabel, useLabelTag, } = this;
        const labelText = required ? label + " *" : label;
        const helperTextId = this.for && getInputHelperTextID(this.for);
        const helperTextClass = {
            helpertext: true,
            "helpertext-normal": !disabled && !readonly,
            "helpertext-readonly": readonly,
        };
        const helperTextSlot = this.el.querySelector("slot[name='helper-text']");
        const labelSlot = this.el.querySelector('[slot="label"]');
        const LabelContent = () => this.isSlotUsed(labelSlot) ? (h("slot", { name: "label" })) : readonly || !useLabelTag ? (h("ic-typography", { variant: "label", class: {
                "readonly-label": readonly,
                "error-label": status === "error" && !(readonly || disabled),
            } }, labelText)) : (h("ic-typography", { variant: "label", class: {
                "readonly-label": readonly,
                "error-label": status === "error" && !(readonly || disabled),
            } }, h("label", { htmlFor: this.for }, labelText)));
        return (h(Host, { key: 'f22dfddc6e1c5dc956e3a2288a168b41ad18b35b', class: {
                "ic-input-label-disabled": !!disabled,
                "ic-input-label-readonly": readonly,
                "with-helper": this.isSlotUsed(helperTextSlot) || helperText !== "",
            } }, !hideLabel && h(LabelContent, { key: 'db3607fc40a7752954f3cd7e40bcf969a6941bdb' }), this.isSlotUsed(helperTextSlot) ? (h("span", { id: helperTextId, class: helperTextClass }, h("slot", { name: "helper-text" }))) : (helperText !== "" && (h("ic-typography", { variant: "caption", class: helperTextClass }, h("span", { id: helperTextId }, helperText))))));
    }
    static get is() { return "ic-input-label"; }
    static get originalStyleUrls() {
        return {
            "$": ["./ic-input-label.css"]
        };
    }
    static get styleUrls() {
        return {
            "$": ["ic-input-label.css"]
        };
    }
    static get properties() {
        return {
            "disabled": {
                "type": "boolean",
                "mutable": false,
                "complexType": {
                    "original": "boolean",
                    "resolved": "boolean | undefined",
                    "references": {}
                },
                "required": false,
                "optional": true,
                "docs": {
                    "tags": [],
                    "text": "If `true`, the disabled state will be set."
                },
                "getter": false,
                "setter": false,
                "attribute": "disabled",
                "reflect": false,
                "defaultValue": "false"
            },
            "for": {
                "type": "string",
                "mutable": false,
                "complexType": {
                    "original": "string",
                    "resolved": "string | undefined",
                    "references": {}
                },
                "required": false,
                "optional": true,
                "docs": {
                    "tags": [],
                    "text": "The ID of the form element the label is bound to."
                },
                "getter": false,
                "setter": false,
                "attribute": "for",
                "reflect": false
            },
            "helperText": {
                "type": "string",
                "mutable": false,
                "complexType": {
                    "original": "string",
                    "resolved": "string",
                    "references": {}
                },
                "required": false,
                "optional": false,
                "docs": {
                    "tags": [],
                    "text": "The helper text that will be displayed."
                },
                "getter": false,
                "setter": false,
                "attribute": "helper-text",
                "reflect": false,
                "defaultValue": "\"\""
            },
            "hideLabel": {
                "type": "boolean",
                "mutable": false,
                "complexType": {
                    "original": "boolean",
                    "resolved": "boolean",
                    "references": {}
                },
                "required": false,
                "optional": false,
                "docs": {
                    "tags": [],
                    "text": "The label will be visually hidden."
                },
                "getter": false,
                "setter": false,
                "attribute": "hide-label",
                "reflect": false,
                "defaultValue": "false"
            },
            "label": {
                "type": "string",
                "mutable": false,
                "complexType": {
                    "original": "string",
                    "resolved": "string",
                    "references": {}
                },
                "required": true,
                "optional": false,
                "docs": {
                    "tags": [],
                    "text": "The text content of the label."
                },
                "getter": false,
                "setter": false,
                "attribute": "label",
                "reflect": false
            },
            "readonly": {
                "type": "boolean",
                "mutable": false,
                "complexType": {
                    "original": "boolean",
                    "resolved": "boolean",
                    "references": {}
                },
                "required": false,
                "optional": false,
                "docs": {
                    "tags": [],
                    "text": "If `true`, the readonly state will be set."
                },
                "getter": false,
                "setter": false,
                "attribute": "readonly",
                "reflect": false,
                "defaultValue": "false"
            },
            "required": {
                "type": "boolean",
                "mutable": false,
                "complexType": {
                    "original": "boolean",
                    "resolved": "boolean",
                    "references": {}
                },
                "required": false,
                "optional": false,
                "docs": {
                    "tags": [],
                    "text": "If `true`, the input label will require a value."
                },
                "getter": false,
                "setter": false,
                "attribute": "required",
                "reflect": false,
                "defaultValue": "false"
            },
            "status": {
                "type": "string",
                "mutable": false,
                "complexType": {
                    "original": "\"error\" | \"\"",
                    "resolved": "\"\" | \"error\"",
                    "references": {}
                },
                "required": false,
                "optional": false,
                "docs": {
                    "tags": [],
                    "text": "The status of the label - e.g. 'error'."
                },
                "getter": false,
                "setter": false,
                "attribute": "status",
                "reflect": false,
                "defaultValue": "\"\""
            },
            "useLabelTag": {
                "type": "boolean",
                "mutable": false,
                "complexType": {
                    "original": "boolean",
                    "resolved": "boolean",
                    "references": {}
                },
                "required": false,
                "optional": false,
                "docs": {
                    "tags": [{
                            "name": "internal",
                            "text": "If `true`, wraps label text in label tag"
                        }],
                    "text": ""
                },
                "getter": false,
                "setter": false,
                "attribute": "use-label-tag",
                "reflect": false,
                "defaultValue": "true"
            }
        };
    }
    static get elementRef() { return "el"; }
}
//# sourceMappingURL=ic-input-label.js.map
