'use strict';

const index = require('./index-75aef097.js');

const daysOfWeek = [
    "Sunday",
    "Monday",
    "Tuesday",
    "Wednesday",
    "Thursday",
    "Friday",
    "Saturday"
];
const daysFromToday = (date) => {
    const today = new Date();
    // To calculate the time difference of two dates
    const timeDifference = today.getTime() - date.getTime();
    // To calculate the no. of days between two dates
    const daysDifference = timeDifference / (1000 * 3600 * 24);
    return Math.floor(daysDifference);
};
// - - - - - - - - - - - -
// Date Functions
// - - - - - - - - - - - -
const isToday = (date) => {
    const today = new Date();
    if (today.toDateString() === date.toDateString()) {
        return true;
    }
    return false;
};
const isYesterday = (date) => {
    const yesterday = new Date();
    yesterday.setDate(yesterday.getDate() - 1);
    if (yesterday.toDateString() === date.toDateString()) {
        return true;
    }
    return false;
};
const formatDate = (date, type = "date-time") => {
    var _a;
    let formattedDate;
    // validate date
    // undefined or null
    if (date === undefined || date === null) {
        return "";
    }
    // If the date object is invalid it will return 'NaN' on getTime() and NaN is never equal to itself
    const dateIsValid = date.getTime() === date.getTime();
    if (!dateIsValid) {
        return "Invalid date";
    }
    let locale = "en-US";
    const lang = (_a = document
        .getElementsByTagName("html")[0]
        .getAttribute("lang")) === null || _a === void 0 ? void 0 : _a.valueOf();
    if (lang === "es") {
        locale = "es-ES";
    }
    formattedDate = date.toLocaleDateString(locale);
    const year = date.getFullYear();
    const month = String(date.getMonth() + 1).padStart(2, "0");
    const day = String(date.getDate()).padStart(2, "0");
    const hours = String(date.getHours()).padStart(2, "0");
    const minutes = String(date.getMinutes()).padStart(2, "0");
    const time = `${hours}:${minutes}`;
    if (type === "date-time") {
        formattedDate += `  ${date.toLocaleTimeString()}`;
    }
    else if (type === "date-time-short") {
        // No seconds
        formattedDate = `${year}/${month}/${day} ${time}`;
    }
    else {
        // pretty
        const daysPassedFromToday = daysFromToday(date);
        const today = new Date();
        const currentYear = today.getFullYear();
        if (isToday(date)) {
            return `Today ${time}`;
        }
        else if (isYesterday(date)) {
            return `Yesterday ${time}`;
        }
        else if (daysPassedFromToday <= 7) {
            // If one week or less, just show the day of the week, and the time
            const day = daysOfWeek[date.getDay()];
            return `${day} ${time}`;
        }
        else if (currentYear === year) {
            // If current year, show day number and month name. Example: November 17th
            if (locale === "es-ES") {
                formattedDate = `${day} de ${date.toLocaleString("es-ES", {
                    month: "long"
                })} ${time}`;
            }
            else {
                formattedDate = `${date.toLocaleString("en-US", {
                    month: "long"
                })} ${day} ${time}`;
            }
        }
        else {
            // Display date as usual
            formattedDate = formatDate(date, "date-time-short");
        }
    }
    return formattedDate;
};
const escapeRegExp = (str) => {
    return str.replace(/[.*+?^${}()|[\]\\]/g, "\\$&");
};
/**
 * @description this function highlights the character(s) that match(es) the filter value.
 * hiChar comes from (hi)light (Char)acters
 * @param text the string to find the filter value on
 * @param filterValue the filter value
 * @returns a span element
 */
const hiChar = function (text, filterValue) {
    if (text && filterValue) {
        const escapedFilterValue = escapeRegExp(filterValue);
        const re = new RegExp(escapedFilterValue, "gi");
        return (index.h("span", { innerHTML: text.replace(re, '<span class="hiChar">$&</span>') }));
    }
    else {
        return text;
    }
};
const mapOptionsToComboBoxItemModel = (options) => options.map(option => ({
    value: option.id,
    caption: option.label
    // startImgSrc: option.iconName
}));
const renderChCheckboxItems = (options, frontEndCheckboxInputHandler, checkedIds) => {
    return options === null || options === void 0 ? void 0 : options.map(option => {
        const value = checkedIds === null || checkedIds === void 0 ? void 0 : checkedIds.find(id => option.value === id || option.id === id);
        return (index.h("ch-checkbox", { name: option.name, class: "checkbox", checkedValue: option.value || option.id, caption: option.label, disabled: option.disabled, value: value, onInput: frontEndCheckboxInputHandler, startImgSrc: option.iconName, key: option.value || option.id }));
    });
};

exports.formatDate = formatDate;
exports.hiChar = hiChar;
exports.mapOptionsToComboBoxItemModel = mapOptionsToComboBoxItemModel;
exports.renderChCheckboxItems = renderChCheckboxItems;

//# sourceMappingURL=helpers-bb6622f5.js.map