"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.ScheduleScheduleWizard = exports.isScheduleValid = void 0;
const tslib_1 = require("tslib");
const React = tslib_1.__importStar(require("react"));
const rebass_1 = require("rebass");
const CheckBox_1 = require("../../../components/CheckBox");
const FormLayout_1 = tslib_1.__importStar(require("../../../components/FormLayout"));
const Radio_1 = tslib_1.__importDefault(require("../../../components/Radio"));
const Tabs_1 = require("../../../components/Tabs");
const ArrayExtensions_1 = tslib_1.__importDefault(require("../../../Utilities/Extensions/ArrayExtensions"));
const AdaptableInput_1 = tslib_1.__importDefault(require("../../Components/AdaptableInput"));
const OnePageAdaptableWizard_1 = require("../../Wizard/OnePageAdaptableWizard");
const isScheduleValid = (schedule, isOneOff) => {
    if (!schedule.Schedule) {
        return 'Schedule is not specified';
    }
    if (schedule.Schedule.Hour === null || schedule.Schedule.Minute === null) {
        return 'Schedule Time is not specified';
    }
    if (isOneOff && !schedule.Schedule.OneOffDate) {
        return 'Schedule Date is not specified';
    }
    if (!isOneOff && ArrayExtensions_1.default.IsEmpty(schedule?.Schedule?.DaysOfWeek)) {
        return 'Schedule Days are not specified';
    }
    return true;
};
exports.isScheduleValid = isScheduleValid;
const daysMap = [
    {
        label: 'Monday',
        value: 'Monday',
    },
    {
        label: 'Tuesday',
        value: 'Tuesday',
    },
    {
        label: 'Wednesday',
        value: 'Wednesday',
    },
    {
        label: 'Thursday',
        value: 'Thursday',
    },
    {
        label: 'Friday',
        value: 'Friday',
    },
    {
        label: 'Saturday',
        value: 'Saturday',
    },
    {
        label: 'Sunday',
        value: 'Sunday',
    },
];
const ScheduleScheduleWizard = (props) => {
    const { data } = (0, OnePageAdaptableWizard_1.useOnePageAdaptableWizardContext)();
    const handleIsOneOfChange = (isOneOffNew) => {
        props.onSetIsOneOff(isOneOffNew);
        const OneOffDate = !isOneOffNew ? null : data?.Schedule?.OneOffDate;
        const DaysOfWeek = isOneOffNew ? [] : data?.Schedule?.DaysOfWeek;
        props.onChange({
            ...data,
            Schedule: {
                ...data.Schedule,
                OneOffDate,
                DaysOfWeek,
            },
        });
    };
    const handleTimeChange = (event) => {
        const [hours, minutes] = (event.target.value || '00:00')
            ?.split(':')
            ?.map((item) => parseInt(item, 10));
        props.onChange({
            ...data,
            Schedule: {
                ...data.Schedule,
                Hour: hours,
                Minute: minutes,
            },
        });
    };
    const handleDayChange = (checked, day) => {
        let newDays = data.Schedule.DaysOfWeek || [];
        if (checked) {
            newDays = [...newDays, day];
        }
        else {
            newDays = newDays.filter((previousDay) => previousDay !== day);
        }
        props.onChange({
            ...data,
            Schedule: {
                ...data.Schedule,
                DaysOfWeek: newDays,
            },
        });
    };
    const handleOneOfDateChange = (event) => {
        props.onChange({
            ...data,
            Schedule: {
                ...data.Schedule,
                OneOffDate: event.target.value,
            },
        });
    };
    let timeValue = '00:00';
    if (typeof data.Schedule?.Hour === 'number' || typeof data?.Schedule?.Minute === 'number') {
        const hours = `${data.Schedule?.Hour}` || '00';
        const minute = `${data.Schedule?.Minute}` || '00';
        timeValue = `${hours.padStart(2, '0')}:${minute.padStart(2, '0')}`;
    }
    return (React.createElement(rebass_1.Box, { "data-name": "schedule-setup" },
        React.createElement(Tabs_1.Tabs, { autoFocus: false },
            React.createElement(Tabs_1.Tabs.Tab, null, "Schedule"),
            React.createElement(Tabs_1.Tabs.Content, null,
                React.createElement(FormLayout_1.default, null,
                    React.createElement(FormLayout_1.FormRow, { label: "Frequency" },
                        React.createElement(Radio_1.default, { "data-name": "frequency-recurring", mr: 2, value: "recurring", checked: !props.isOneOff, onChange: (checked) => checked && handleIsOneOfChange(false) }, "Recurring Days"),
                        React.createElement(Radio_1.default, { "data-name": "frequency-one-off", checked: props.isOneOff, value: "oneoff", onChange: (checked) => checked && handleIsOneOfChange(true) }, "One-Off Date")),
                    props.isOneOff ? (React.createElement(FormLayout_1.FormRow, { label: "Date" },
                        React.createElement(AdaptableInput_1.default, { "data-name": "one-off-date", width: 300, type: "date", placeholder: "Date", onChange: handleOneOfDateChange, value: data.Schedule?.OneOffDate || '' }))) : (React.createElement(FormLayout_1.FormRow, { label: "Days" }, daysMap.map((day) => (React.createElement(CheckBox_1.CheckBox, { "data-name": `day-${day.value}`, width: "33%", ml: 2, value: day.value, checked: data.Schedule?.DaysOfWeek?.includes(day.value), key: day.value, onChange: (checked) => handleDayChange(checked, day.value) }, day.label))))),
                    React.createElement(FormLayout_1.FormRow, { label: "Time" },
                        React.createElement(AdaptableInput_1.default, { "data-name": "time", width: 300, value: timeValue, type: "time", onChange: handleTimeChange })))))));
};
exports.ScheduleScheduleWizard = ScheduleScheduleWizard;
