import { storeActions } from "@/constants/store-actions";
import baseMixin from "@/mixins/base-mixin.js";

export async function getAlertReasons(ctu) {
    const alertReasons = await baseMixin.methods.dispatchStoreActionWithLogging(
        storeActions.GET_ALERT_REASONS_BY_CTU,
        {
            ctu: ctu,
        },
        "schedule"
    );

    return Promise.resolve(alertReasons);
}

export function calcDaysBetweenDates(dateString1, dateString2) {
    const date1 = new Date(dateString1);
    const date2 = new Date(dateString2);
    const timeDifference = Math.abs(date2 - date1); // Calculate the time difference in milliseconds
    return Math.ceil(timeDifference / (1000 * 60 * 60 * 24)); // Convert milliseconds to days
}

export function convertDateToDateString(date) {
    // returns YYYY-MM-DD format
    if (date instanceof Date !== true) return;
    return (
        date.getFullYear() +
        "-" +
        ("0" + (date.getMonth() + 1)).slice(-2) +
        "-" +
        ("0" + date.getDate()).slice(-2)
    );
}

export function convertDateStringToDate(dateString) {
    // dateString must be YYYY-MM-DD format
    if (typeof dateString !== "string") return;
    const dateParts = dateString.split("-");
    return new Date(dateParts[0], parseInt(dateParts[1]) - 1, dateParts[2]);
}

export function sumDateString(dateString, daysToAdd) {
    // dateString must be YYYY-MM-DD format
    if (typeof dateString !== "string") return;
    const date = convertDateStringToDate(dateString);
    date.setDate(date.getDate() + daysToAdd);
    return convertDateToDateString(date);
}

export function militaryToTwelveHourTime(timeString) {
    // Expected input: "HH:MM"
    if (typeof timeString !== "string") return;
    let hours = parseInt(timeString.split(":")[0]);
    const minutes = timeString.split(":")[1];
    const meridianNotation = hours > 11 ? "PM" : "AM";

    if (hours > 12) {
        hours -= 12;
    }

    return `${hours}:${minutes} ${meridianNotation}`;
}
