import { h } from "@stencil/core";
import { PlatformServiceName, } from "@limetech/lime-web-components";
import { toServerDate } from "./to-server-date";
/**
 * When developing apps, add-ons, or solution for Lime CRM,
 * you may need date pickers in the UI.
 * :::note
 * This date picker provides support for Lime CRM's user preferences.
 * So instead of using `limel-date-picker`, use this component `limebb-date-picker`.
 * This will ensure that the format which the end-user has chosen is actually used
 * to display the date value.
 * :::
 *
 * @exampleComponent limebb-example-date-picker-datetime
 * @exampleComponent limebb-example-date-picker-date
 * @exampleComponent limebb-example-date-picker-time
 * @exampleComponent limebb-example-date-picker-week
 */
export class DatePicker {
    constructor() {
        /**
         * Set to `true` to disable the field.
         * Use `disabled` to indicate that the field can normally be interacted
         * with, but is currently disabled. This tells the user that if certain
         * requirements are met, the field may become enabled again.
         */
        this.disabled = false;
        /**
         * Set to `true` to make the field read-only.
         * Use `readonly` when the field is only there to present the data it holds,
         * and will not become possible for the current user to edit.
         */
        this.readonly = false;
        /**
         * Set to `true` to indicate that the current value of the date picker is
         * invalid.
         */
        this.invalid = false;
        /**
         * Set to `true` to indicate that the field is required.
         */
        this.required = false;
        /**
         * Type of date picker.
         */
        this.type = 'datetime';
        this.shouldEmitValueAsString = true;
        this.handleChange = (event) => {
            if (!event.detail) {
                return;
            }
            event.stopPropagation();
            const date = toServerDate(event.detail, this.type);
            if (this.shouldEmitValueAsString) {
                this.change.emit(date.toISOString());
                return;
            }
            this.change.emit(date);
        };
        this.formatter = (value) => this.dateTimeFormatter.format(value, dateTimePropertyTypes[this.type] || this.type);
    }
    render() {
        if (!this.language) {
            this.language = this.getAppLanguage();
        }
        if (this.value) {
            this.shouldEmitValueAsString = typeof this.value === 'string';
        }
        return (h("limel-date-picker", { key: '9a27ee751c1928a442651e8473b0c588778dbdc0', disabled: this.disabled, readonly: this.readonly, invalid: this.invalid, label: this.label, placeholder: this.placeholder, helperText: this.helperText, required: this.required, value: this.parseDateValue(), type: this.type, language: this.language, formatter: this.formatter, onChange: this.handleChange }));
    }
    parseDateValue() {
        const value = this.value;
        if (!value) {
            return;
        }
        return this.dateTimeFormatter.parse(value, dateTimePropertyTypes[this.type] || this.type);
    }
    get dateTimeFormatter() {
        return this.platform.get(PlatformServiceName.DateTimeFormatter);
    }
    getAppLanguage() {
        const translator = this.platform.get(PlatformServiceName.Translate);
        const application = this.platform.get(PlatformServiceName.Application);
        let language;
        if (hasLanguage(translator)) {
            language = translator.getLanguage();
        }
        else {
            language = application.getLanguage();
        }
        if (isSupportedLanguage(language)) {
            return language;
        }
        return 'en';
    }
    static get is() { return "limebb-date-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
            },
            "disabled": {
                "type": "boolean",
                "mutable": false,
                "complexType": {
                    "original": "boolean",
                    "resolved": "boolean",
                    "references": {}
                },
                "required": false,
                "optional": false,
                "docs": {
                    "tags": [],
                    "text": "Set to `true` to disable the field.\nUse `disabled` to indicate that the field can normally be interacted\nwith, but is currently disabled. This tells the user that if certain\nrequirements are met, the field may become enabled again."
                },
                "getter": false,
                "setter": false,
                "reflect": true,
                "attribute": "disabled",
                "defaultValue": "false"
            },
            "readonly": {
                "type": "boolean",
                "mutable": false,
                "complexType": {
                    "original": "boolean",
                    "resolved": "boolean",
                    "references": {}
                },
                "required": false,
                "optional": false,
                "docs": {
                    "tags": [],
                    "text": "Set to `true` to make the field read-only.\nUse `readonly` when the field is only there to present the data it holds,\nand will not become possible for the current user to edit."
                },
                "getter": false,
                "setter": false,
                "reflect": true,
                "attribute": "readonly",
                "defaultValue": "false"
            },
            "invalid": {
                "type": "boolean",
                "mutable": false,
                "complexType": {
                    "original": "boolean",
                    "resolved": "boolean",
                    "references": {}
                },
                "required": false,
                "optional": false,
                "docs": {
                    "tags": [],
                    "text": "Set to `true` to indicate that the current value of the date picker is\ninvalid."
                },
                "getter": false,
                "setter": false,
                "reflect": true,
                "attribute": "invalid",
                "defaultValue": "false"
            },
            "label": {
                "type": "string",
                "mutable": false,
                "complexType": {
                    "original": "string",
                    "resolved": "string",
                    "references": {}
                },
                "required": false,
                "optional": false,
                "docs": {
                    "tags": [],
                    "text": "Text to display next to the date picker"
                },
                "getter": false,
                "setter": false,
                "reflect": true,
                "attribute": "label"
            },
            "placeholder": {
                "type": "string",
                "mutable": false,
                "complexType": {
                    "original": "string",
                    "resolved": "string",
                    "references": {}
                },
                "required": false,
                "optional": false,
                "docs": {
                    "tags": [],
                    "text": "The placeholder text shown inside the input field, when the field is focused and empty"
                },
                "getter": false,
                "setter": false,
                "reflect": true,
                "attribute": "placeholder"
            },
            "helperText": {
                "type": "string",
                "mutable": false,
                "complexType": {
                    "original": "string",
                    "resolved": "string",
                    "references": {}
                },
                "required": false,
                "optional": false,
                "docs": {
                    "tags": [],
                    "text": "Optional helper text to display below the input field when it has focus"
                },
                "getter": false,
                "setter": false,
                "reflect": true,
                "attribute": "helper-text"
            },
            "required": {
                "type": "boolean",
                "mutable": false,
                "complexType": {
                    "original": "boolean",
                    "resolved": "boolean",
                    "references": {}
                },
                "required": false,
                "optional": false,
                "docs": {
                    "tags": [],
                    "text": "Set to `true` to indicate that the field is required."
                },
                "getter": false,
                "setter": false,
                "reflect": true,
                "attribute": "required",
                "defaultValue": "false"
            },
            "value": {
                "type": "string",
                "mutable": false,
                "complexType": {
                    "original": "string | Date",
                    "resolved": "Date | string",
                    "references": {
                        "Date": {
                            "location": "global",
                            "id": "global::Date"
                        }
                    }
                },
                "required": false,
                "optional": false,
                "docs": {
                    "tags": [],
                    "text": "The value of the field."
                },
                "getter": false,
                "setter": false,
                "reflect": false,
                "attribute": "value"
            },
            "type": {
                "type": "string",
                "mutable": false,
                "complexType": {
                    "original": "DateType",
                    "resolved": "\"date\" | \"datetime\" | \"month\" | \"quarter\" | \"time\" | \"week\" | \"year\"",
                    "references": {
                        "DateType": {
                            "location": "import",
                            "path": "@limetech/lime-elements",
                            "id": "node_modules::DateType",
                            "referenceLocation": "DateType"
                        }
                    }
                },
                "required": false,
                "optional": false,
                "docs": {
                    "tags": [],
                    "text": "Type of date picker."
                },
                "getter": false,
                "setter": false,
                "reflect": true,
                "attribute": "type",
                "defaultValue": "'datetime'"
            }
        };
    }
    static get events() {
        return [{
                "method": "change",
                "name": "change",
                "bubbles": true,
                "cancelable": true,
                "composed": true,
                "docs": {
                    "tags": [],
                    "text": "Emitted when the date picker value is changed."
                },
                "complexType": {
                    "original": "string | Date | null",
                    "resolved": "Date | null | string",
                    "references": {
                        "Date": {
                            "location": "global",
                            "id": "global::Date"
                        }
                    }
                }
            }];
    }
}
const dateTimePropertyTypes = {
    datetime: 'time',
    time: 'timeofday',
};
const supportedLanguages = new Set([
    'da',
    'de',
    'en',
    'fi',
    'fr',
    'nb',
    'no',
    'nl',
    'sv',
]);
const isSupportedLanguage = (language) => supportedLanguages.has(language);
function hasLanguage(service) {
    return typeof (service === null || service === void 0 ? void 0 : service.getLanguage) === 'function';
}
