import { h } from "@stencil/core";
import { PlatformServiceName, } from "@limetech/lime-web-components";
import { getAllLanguages, getLanguageTitle } from "./language";
import { sortBy } from "lodash-es";
/**
 * This component enables you to select supported languages.
 * The selected language code will be emitted by the component - as a string if multipleChoice={false}
 * or as an array of strings if multipleChoice={true}.
 *
 * @exampleComponent limebb-example-locale-picker
 * @exampleComponent limebb-example-locale-picker-multiple
 */
export class LocalePicker {
    constructor() {
        /**
         * @inheritdoc
         */
        this.context = { limetype: null, id: null };
        this.handleChange = (event) => {
            event.stopPropagation();
            if (this.multipleChoice) {
                this.change.emit(event.detail.map((option) => option.value));
            }
            else {
                this.change.emit(event.detail.value);
            }
        };
    }
    async componentWillLoad() {
        try {
            this.http = this.platform.get(PlatformServiceName.Http);
            this.allLanguages = await getAllLanguages(this.http);
        }
        catch (error) {
            // Throwing an error here will break the entire page, so we just log it instead
            console.error(error);
        }
    }
    getOptions() {
        return this.allLanguages.map((value) => ({
            value: value,
            text: getLanguageTitle(value, value),
        }));
    }
    render() {
        const options = this.getOptions();
        let selectedOptions;
        if (Array.isArray(this.value)) {
            const value = this.value;
            selectedOptions = options.filter((option) => value.includes(option.value));
        }
        else {
            const value = this.value;
            selectedOptions = options.find((option) => option.value === value);
        }
        return (h("limel-select", { key: 'b5aad60b10237826f58f096a343beb7fcbfe2b9b', value: selectedOptions, options: sortBy(options, 'text'), helperText: this.helperText, label: this.label, required: this.required, disabled: this.disabled, readonly: this.readonly, onChange: this.handleChange, multiple: this.multipleChoice }));
    }
    static get is() { return "limebb-locale-picker"; }
    static get encapsulation() { return "shadow"; }
    static get properties() {
        return {
            "platform": {
                "type": "unknown",
                "mutable": false,
                "complexType": {
                    "original": "LimeWebComponentPlatform",
                    "resolved": "LimeWebComponentPlatform",
                    "references": {
                        "LimeWebComponentPlatform": {
                            "location": "import",
                            "path": "@limetech/lime-web-components",
                            "id": "node_modules::LimeWebComponentPlatform",
                            "referenceLocation": "LimeWebComponentPlatform"
                        }
                    }
                },
                "required": true,
                "optional": false,
                "docs": {
                    "tags": [{
                            "name": "inheritdoc",
                            "text": undefined
                        }],
                    "text": "Reference to the platform service container.\n\nUse this to access all platform services like repositories, HTTP client,\ncommand bus, navigator, and more. The platform instance is shared across\nall components in the same application context."
                },
                "getter": false,
                "setter": false
            },
            "context": {
                "type": "unknown",
                "mutable": false,
                "complexType": {
                    "original": "LimeWebComponentContext",
                    "resolved": "LimeWebComponentContext",
                    "references": {
                        "LimeWebComponentContext": {
                            "location": "import",
                            "path": "@limetech/lime-web-components",
                            "id": "node_modules::LimeWebComponentContext",
                            "referenceLocation": "LimeWebComponentContext"
                        }
                    }
                },
                "required": false,
                "optional": false,
                "docs": {
                    "tags": [{
                            "name": "inheritdoc",
                            "text": undefined
                        }],
                    "text": "The context describing where this component is running.\n\nThe context provides information about the current limetype and record ID\n(if viewing/editing a specific record). Components can use this to load\nrelevant data and understand their operating environment."
                },
                "getter": false,
                "setter": false,
                "defaultValue": "{ limetype: null, id: null }"
            },
            "value": {
                "type": "string",
                "mutable": false,
                "complexType": {
                    "original": "LanguagePickerType",
                    "resolved": "string | string[]",
                    "references": {
                        "LanguagePickerType": {
                            "location": "import",
                            "path": "./language-picker.types",
                            "id": "src/components/locale-picker/language-picker.types.ts::LanguagePickerType",
                            "referenceLocation": "LanguagePickerType"
                        }
                    }
                },
                "required": false,
                "optional": false,
                "docs": {
                    "tags": [{
                            "name": "inheritdoc",
                            "text": undefined
                        }],
                    "text": "The value of the current property"
                },
                "getter": false,
                "setter": false,
                "reflect": false,
                "attribute": "value"
            },
            "required": {
                "type": "boolean",
                "mutable": false,
                "complexType": {
                    "original": "boolean",
                    "resolved": "boolean | undefined",
                    "references": {}
                },
                "required": false,
                "optional": true,
                "docs": {
                    "tags": [{
                            "name": "inheritdoc",
                            "text": undefined
                        }],
                    "text": "Whether or not the current property is required"
                },
                "getter": false,
                "setter": false,
                "reflect": false,
                "attribute": "required"
            },
            "disabled": {
                "type": "boolean",
                "mutable": false,
                "complexType": {
                    "original": "boolean",
                    "resolved": "boolean | undefined",
                    "references": {}
                },
                "required": false,
                "optional": true,
                "docs": {
                    "tags": [{
                            "name": "inheritdoc",
                            "text": undefined
                        }],
                    "text": "Whether or not the current property is disabled"
                },
                "getter": false,
                "setter": false,
                "reflect": false,
                "attribute": "disabled"
            },
            "label": {
                "type": "string",
                "mutable": false,
                "complexType": {
                    "original": "string",
                    "resolved": "string | undefined",
                    "references": {}
                },
                "required": false,
                "optional": true,
                "docs": {
                    "tags": [{
                            "name": "inheritdoc",
                            "text": undefined
                        }],
                    "text": "The label of the current property"
                },
                "getter": false,
                "setter": false,
                "reflect": false,
                "attribute": "label"
            },
            "helperText": {
                "type": "string",
                "mutable": false,
                "complexType": {
                    "original": "string",
                    "resolved": "string | undefined",
                    "references": {}
                },
                "required": false,
                "optional": true,
                "docs": {
                    "tags": [{
                            "name": "inheritdoc",
                            "text": undefined
                        }],
                    "text": "The helper text for the current property"
                },
                "getter": false,
                "setter": false,
                "reflect": false,
                "attribute": "helper-text"
            },
            "readonly": {
                "type": "boolean",
                "mutable": false,
                "complexType": {
                    "original": "boolean",
                    "resolved": "boolean | undefined",
                    "references": {}
                },
                "required": false,
                "optional": true,
                "docs": {
                    "tags": [{
                            "name": "inheritdoc",
                            "text": undefined
                        }],
                    "text": "Whether or not the current property is readonly"
                },
                "getter": false,
                "setter": false,
                "reflect": false,
                "attribute": "readonly"
            },
            "multipleChoice": {
                "type": "boolean",
                "mutable": false,
                "complexType": {
                    "original": "boolean",
                    "resolved": "boolean | undefined",
                    "references": {}
                },
                "required": false,
                "optional": true,
                "docs": {
                    "tags": [],
                    "text": "Whether the multiple selection is enabled"
                },
                "getter": false,
                "setter": false,
                "reflect": false,
                "attribute": "multiple-choice"
            }
        };
    }
    static get states() {
        return {
            "allLanguages": {}
        };
    }
    static get events() {
        return [{
                "method": "change",
                "name": "change",
                "bubbles": true,
                "cancelable": true,
                "composed": true,
                "docs": {
                    "tags": [{
                            "name": "inheritdoc",
                            "text": undefined
                        }],
                    "text": "The event to emit when the value of the current property has changed"
                },
                "complexType": {
                    "original": "LanguagePickerType",
                    "resolved": "string | string[]",
                    "references": {
                        "LanguagePickerType": {
                            "location": "import",
                            "path": "./language-picker.types",
                            "id": "src/components/locale-picker/language-picker.types.ts::LanguagePickerType",
                            "referenceLocation": "LanguagePickerType"
                        }
                    }
                }
            }];
    }
}
