'use strict';

var index = require('./index-C_OnxXhP.js');
var index_esm = require('./index.esm-C1i_NocP.js');

/**
 * Create a UTC date with the same values for year, month and day as the given
 * date, but with the time component set to 00:00:00.
 *
 * Example:
 * input: 2020-01-01T00:00:00+02:00
 * output: 2020-01-01T00:00:00Z
 *
 * @param changedDate The date to convert
 * @param type The type of date
 * @returns The converted date
 */
function toServerDate(changedDate, type) {
    if (type === 'datetime' || type === 'time') {
        // These types should retain their time component
        return changedDate;
    }
    return new Date(Date.UTC(changedDate.getFullYear(), changedDate.getMonth(), changedDate.getDate()));
}

const DatePicker = class {
    constructor(hostRef) {
        index.registerInstance(this, hostRef);
        this.change = index.createEvent(this, "change");
        /**
         * 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 (index.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(index_esm.n.DateTimeFormatter);
    }
    getAppLanguage() {
        const translator = this.platform.get(index_esm.n.Translate);
        const application = this.platform.get(index_esm.n.Application);
        let language;
        if (hasLanguage(translator)) {
            language = translator.getLanguage();
        }
        else {
            language = application.getLanguage();
        }
        if (isSupportedLanguage(language)) {
            return language;
        }
        return 'en';
    }
};
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';
}

exports.limebb_date_picker = DatePicker;
