import { h } from "@stencil/core";
import { randomIdGenerator } from "../../../global/utils/randomIdGenerator";
import { logger } from "../../../global/utils/logger";
import { _t } from "../../../text";
/**
 * @slot actions - Förväntar sig interaktiva och ikonstora komponenter
 * @swedishName Etikett
 */
export class FormLabel {
    constructor() {
        this.labelText = undefined;
        this.hasActionSlot = undefined;
        this.afLabel = undefined;
        this.afId = randomIdGenerator('digi-form-label');
        this.afFor = undefined;
        this.afDescription = undefined;
        this.afRequired = undefined;
        this.afAnnounceIfOptional = false;
        this.afRequiredText = _t.required.toLowerCase();
        this.afAnnounceIfOptionalText = _t.optional.toLowerCase();
    }
    componentWillLoad() {
        this.setLabelText();
        this.validateFor();
        this.handleSlotVisibility();
    }
    validateFor() {
        if (this.afFor == null) {
            logger.warn(`digi-form-label must have a for attribute. Please add a for attribute using af-for`, this.hostElement);
        }
    }
    setLabelText() {
        this.labelText = `${this.afLabel} ${this.requiredText}`;
        if (!this.afLabel) {
            logger.warn(`digi-form-label must have a label. Please add a label using af-label`, this.hostElement);
        }
    }
    handleSlotVisibility() {
        this.hasActionSlot = !!this.hostElement.querySelector('[slot="actions"]');
    }
    get requiredText() {
        if (this.afRequired && !this.afAnnounceIfOptional) {
            return this.afRequiredText ? ` (${this.afRequiredText})` : '';
        }
        else if (!this.afRequired && this.afAnnounceIfOptional) {
            return this.afAnnounceIfOptionalText
                ? ` (${this.afAnnounceIfOptionalText})`
                : '';
        }
        else {
            return '';
        }
    }
    render() {
        return (h("div", { key: '0714a446215f218b8cc5449a318090a7e15de36e', class: "digi-form-label" }, h("div", { key: '290f19411233736a2741ccff9f95a328f0181513', class: "digi-form-label__label-group" }, this.afFor && this.afLabel && (h("label", { key: '9c990f78e47ee4bc1de6cc750cea54f8eb763033', class: "digi-form-label__label", htmlFor: this.afFor, id: this.afId }, h("span", { key: '22a428a427bb72921dd9b2487f2fb97844db541f', class: "digi-form-label__label--label" }, this.labelText), this.afDescription && (h("span", { key: 'd13261b4e0525e5fd680032cd625c579d8037302', class: "digi-form-label__label--description" }, this.afDescription)))), this.hasActionSlot && (h("div", { key: '198faa5a754472ca76e2e84edf38f6ea1280b067', class: "digi-form-label__actions" }, h("slot", { key: '0066fc1f5cab5b72c2971e50cb8f0b1736d24ec7', name: "actions" }))))));
    }
    static get is() { return "digi-form-label"; }
    static get encapsulation() { return "scoped"; }
    static get originalStyleUrls() {
        return {
            "$": ["form-label.scss"]
        };
    }
    static get styleUrls() {
        return {
            "$": ["form-label.css"]
        };
    }
    static get properties() {
        return {
            "afLabel": {
                "type": "string",
                "mutable": false,
                "complexType": {
                    "original": "string",
                    "resolved": "string",
                    "references": {}
                },
                "required": true,
                "optional": false,
                "docs": {
                    "tags": [{
                            "name": "en",
                            "text": "The label text"
                        }],
                    "text": "Texten till labelelementet"
                },
                "getter": false,
                "setter": false,
                "attribute": "af-label",
                "reflect": false
            },
            "afId": {
                "type": "string",
                "mutable": false,
                "complexType": {
                    "original": "string",
                    "resolved": "string",
                    "references": {}
                },
                "required": false,
                "optional": false,
                "docs": {
                    "tags": [{
                            "name": "en",
                            "text": "Set id attribute. Defaults to random string."
                        }],
                    "text": "S\u00E4tter attributet 'id'. Om inget v\u00E4ljs s\u00E5 skapas ett slumpm\u00E4ssigt id."
                },
                "getter": false,
                "setter": false,
                "attribute": "af-id",
                "reflect": false,
                "defaultValue": "randomIdGenerator('digi-form-label')"
            },
            "afFor": {
                "type": "string",
                "mutable": false,
                "complexType": {
                    "original": "string",
                    "resolved": "string",
                    "references": {}
                },
                "required": true,
                "optional": false,
                "docs": {
                    "tags": [{
                            "name": "en",
                            "text": "Set for attribute. Should be identical with the id attribute of the associated input element."
                        }],
                    "text": "S\u00E4tter attributet 'for'. Ska vara identiskt med attributet 'id' p\u00E5 inputelementet"
                },
                "getter": false,
                "setter": false,
                "attribute": "af-for",
                "reflect": false
            },
            "afDescription": {
                "type": "string",
                "mutable": false,
                "complexType": {
                    "original": "string",
                    "resolved": "string",
                    "references": {}
                },
                "required": false,
                "optional": false,
                "docs": {
                    "tags": [{
                            "name": "en",
                            "text": "A description text"
                        }],
                    "text": "Valfri beskrivande text"
                },
                "getter": false,
                "setter": false,
                "attribute": "af-description",
                "reflect": false
            },
            "afRequired": {
                "type": "boolean",
                "mutable": false,
                "complexType": {
                    "original": "boolean",
                    "resolved": "boolean",
                    "references": {}
                },
                "required": false,
                "optional": false,
                "docs": {
                    "tags": [{
                            "name": "en",
                            "text": "Set to true if the associated input element is required"
                        }],
                    "text": "Aktivera om det tillh\u00F6rande inputelementet \u00E4r obligatoriskt"
                },
                "getter": false,
                "setter": false,
                "attribute": "af-required",
                "reflect": false
            },
            "afAnnounceIfOptional": {
                "type": "boolean",
                "mutable": false,
                "complexType": {
                    "original": "boolean",
                    "resolved": "boolean",
                    "references": {}
                },
                "required": false,
                "optional": false,
                "docs": {
                    "tags": [{
                            "name": "en",
                            "text": "Set this to true if the form contains more required fields than optional fields."
                        }],
                    "text": "S\u00E4tt denna till true om formul\u00E4ret inneh\u00E5ller fler obligatoriska f\u00E4lt \u00E4n valfria."
                },
                "getter": false,
                "setter": false,
                "attribute": "af-announce-if-optional",
                "reflect": false,
                "defaultValue": "false"
            },
            "afRequiredText": {
                "type": "string",
                "mutable": false,
                "complexType": {
                    "original": "string",
                    "resolved": "string",
                    "references": {}
                },
                "required": false,
                "optional": false,
                "docs": {
                    "tags": [{
                            "name": "en",
                            "text": "Set text for afRequired."
                        }],
                    "text": "S\u00E4tter text f\u00F6r afRequired."
                },
                "getter": false,
                "setter": false,
                "attribute": "af-required-text",
                "reflect": false,
                "defaultValue": "_t.required.toLowerCase()"
            },
            "afAnnounceIfOptionalText": {
                "type": "string",
                "mutable": false,
                "complexType": {
                    "original": "string",
                    "resolved": "string",
                    "references": {}
                },
                "required": false,
                "optional": false,
                "docs": {
                    "tags": [{
                            "name": "en",
                            "text": "Set text for afAnnounceIfOptional."
                        }],
                    "text": "S\u00E4tter text f\u00F6r afAnnounceIfOptional."
                },
                "getter": false,
                "setter": false,
                "attribute": "af-announce-if-optional-text",
                "reflect": false,
                "defaultValue": "_t.optional.toLowerCase()"
            }
        };
    }
    static get states() {
        return {
            "labelText": {},
            "hasActionSlot": {}
        };
    }
    static get elementRef() { return "hostElement"; }
    static get watchers() {
        return [{
                "propName": "afFor",
                "methodName": "validateFor"
            }, {
                "propName": "afLabel",
                "methodName": "setLabelText"
            }, {
                "propName": "afRequired",
                "methodName": "setLabelText"
            }, {
                "propName": "afAnnounceIfOptional",
                "methodName": "setLabelText"
            }];
    }
}
