/**
 * Copyright Aquera Inc 2023
 *
 * This source code is licensed under the BSD-3-Clause license found in the
 * LICENSE file in the root directory of this source tree.
 */
import { __decorate } from "tslib";
import { html } from 'lit';
import { customElement, property } from 'lit/decorators.js';
import { styles } from './nile-format-date.css';
import NileElement from '../internal/nile-element';
/**
 * Nile format-date component.
 *
 * @tag nile-format-date
 *
 */
let NileFormatDate = class NileFormatDate extends NileElement {
    constructor() {
        super(...arguments);
        /**
         * The date/time to format. If not set, the current date and time will be used. When passing a string, it's strongly
         * recommended to use the ISO 8601 format to ensure timezones are handled correctly. To convert a date to this format
         * in JavaScript, use [`date.toISOString()`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date/toISOString).
         */
        this.date = new Date();
        /** The format for displaying the hour. */
        this.hourFormat = 'auto';
        /** The separator for the date format. */
        this.dateSeparator = '-';
    }
    /**
     * The styles for nile-format-date
     * @remarks If you are extending this class you can extend the base styles with super. Eg `return [super(), myCustomStyles]`
     */
    static get styles() {
        return [styles];
    }
    formatDate(dateToFormat, options) {
        dateToFormat = new Date(dateToFormat);
        const formatter = new Intl.DateTimeFormat('en-GB', options);
        const parts = formatter.formatToParts(dateToFormat);
        const formattedDate = parts.map(({ type, value }) => {
            if (type === 'day' || type === 'month' || type === 'year') {
                return value;
            }
            if (type === 'literal' && value === '/') {
                return this.dateSeparator;
            }
            return value;
        }).join('');
        return formattedDate;
    }
    // formatDate(dateToFormat: Date | string, options?: Intl.DateTimeFormatOptions): string {
    //   dateToFormat = new Date(dateToFormat);
    //   return new Intl.DateTimeFormat('en-GB', options).format(dateToFormat);
    // }
    render() {
        const date = new Date(this.date);
        const hour12 = this.hourFormat === 'auto' ? undefined : this.hourFormat === '12';
        // Check for an invalid date
        if (isNaN(date.getMilliseconds())) {
            return undefined;
        }
        return html `
      <time datetime=${date.toISOString()}>
        ${this.formatDate(date, {
            weekday: this.weekday,
            era: this.era,
            year: this.year,
            month: this.month,
            day: this.day,
            hour: this.hour,
            minute: this.minute,
            second: this.second,
            timeZoneName: this.timeZoneName,
            timeZone: this.timeZone,
            hour12: hour12,
        })}
      </time>
    `;
    }
};
__decorate([
    property()
], NileFormatDate.prototype, "date", void 0);
__decorate([
    property()
], NileFormatDate.prototype, "weekday", void 0);
__decorate([
    property()
], NileFormatDate.prototype, "era", void 0);
__decorate([
    property()
], NileFormatDate.prototype, "year", void 0);
__decorate([
    property()
], NileFormatDate.prototype, "month", void 0);
__decorate([
    property()
], NileFormatDate.prototype, "day", void 0);
__decorate([
    property()
], NileFormatDate.prototype, "hour", void 0);
__decorate([
    property()
], NileFormatDate.prototype, "minute", void 0);
__decorate([
    property()
], NileFormatDate.prototype, "second", void 0);
__decorate([
    property({ attribute: 'time-zone-name' })
], NileFormatDate.prototype, "timeZoneName", void 0);
__decorate([
    property({ attribute: 'time-zone' })
], NileFormatDate.prototype, "timeZone", void 0);
__decorate([
    property({ attribute: 'hour-format' })
], NileFormatDate.prototype, "hourFormat", void 0);
__decorate([
    property({ attribute: 'date-separator' })
], NileFormatDate.prototype, "dateSeparator", void 0);
NileFormatDate = __decorate([
    customElement('nile-format-date')
], NileFormatDate);
export { NileFormatDate };
/* #endregion */
export default NileFormatDate;
//# sourceMappingURL=nile-format-date.js.map