"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.ScheduleWizard = void 0;
const tslib_1 = require("tslib");
const React = tslib_1.__importStar(require("react"));
const rebass_1 = require("rebass");
const OnePageAdaptableWizard_1 = require("../../../View/Wizard/OnePageAdaptableWizard");
const ScheduleSettingsWizard_1 = require("./ScheduleSettingsWizard");
const ScheduleScheduleWizard_1 = require("./ScheduleScheduleWizard");
const ScheduleScheduleSummary_1 = require("./ScheduleScheduleSummary");
const Enums_1 = require("../../../AdaptableState/Common/Enums");
const ObjectFactory_1 = tslib_1.__importDefault(require("../../../Utilities/ObjectFactory"));
const react_redux_1 = require("react-redux");
const ScheduleRedux = tslib_1.__importStar(require("../../../Redux/ActionsReducers/ScheduleRedux"));
const ObjectTagsWizardSection_1 = require("../../Wizard/ObjectTagsWizardSection");
const ScheduleWizard = (props) => {
    const data = props.data ?? props.popupParams?.value;
    const isNew = props.popupParams?.action ? props.popupParams.action === 'New' : !props.data;
    const scheduleType = data ? data.ScheduleType : props?.abObjectType?.name;
    const [schedule, setSchedule] = React.useState(() => {
        if (data) {
            return data;
        }
        let scheduleState = null;
        switch (scheduleType) {
            case Enums_1.ScheduleType.Reminder:
                scheduleState = ObjectFactory_1.default.CreateEmptyReminderSchedule();
                break;
            case Enums_1.ScheduleType.Report:
                scheduleState = ObjectFactory_1.default.CreateEmptyReportSchedule();
                break;
            case Enums_1.ScheduleType.ipushpull:
                scheduleState = ObjectFactory_1.default.CreateEmptyIPushPullSchedule();
                break;
            case Enums_1.ScheduleType.OpenFin:
                scheduleState = ObjectFactory_1.default.CreateEmptyOpenFinSchedule();
                break;
        }
        return scheduleState;
    });
    const [isOneOff, setIsOneOff] = React.useState(false);
    const dispatch = (0, react_redux_1.useDispatch)();
    const handleFinish = () => {
        let action = null;
        if (!isNew) {
            switch (scheduleType) {
                case Enums_1.ScheduleType.Reminder:
                    action = ScheduleRedux.ReminderScheduleEdit(schedule);
                    break;
                case Enums_1.ScheduleType.Report:
                    action = ScheduleRedux.ReportScheduleEdit(schedule);
                    break;
                case Enums_1.ScheduleType.ipushpull:
                    action = ScheduleRedux.IPushPullScheduleEdit(schedule);
                    break;
                    break;
                case Enums_1.ScheduleType.OpenFin:
                    action = ScheduleRedux.OpenFinScheduleEdit(schedule);
                    break;
            }
        }
        else {
            switch (scheduleType) {
                case Enums_1.ScheduleType.Reminder:
                    action = ScheduleRedux.ReminderScheduleAdd(schedule);
                    break;
                case Enums_1.ScheduleType.Report:
                    action = ScheduleRedux.ReportScheduleAdd(schedule);
                    break;
                case Enums_1.ScheduleType.ipushpull:
                    action = ScheduleRedux.IPushPullScheduleAdd(schedule);
                    break;
                case Enums_1.ScheduleType.OpenFin:
                    action = ScheduleRedux.OpenFinScheduleAdd(schedule);
                    break;
            }
        }
        dispatch(action);
        props.onFinishWizard(schedule);
    };
    return (React.createElement(OnePageAdaptableWizard_1.OnePageAdaptableWizard, { defaultCurrentSectionName: props.defaultCurrentSectionName, moduleInfo: props.moduleInfo, data: schedule, onHide: props.onCloseWizard, onFinish: handleFinish, sections: [
            {
                title: 'Settings',
                isValid: ScheduleSettingsWizard_1.isSettingsValid,
                renderSummary: () => React.createElement(ScheduleSettingsWizard_1.ScheduleSettingsSummary, null),
                render: () => (React.createElement(rebass_1.Box, { p: 3 },
                    React.createElement(ScheduleSettingsWizard_1.ScheduleSettingsWizard, { onChange: setSchedule }))),
            },
            {
                title: 'Schedule',
                isValid: (data) => (0, ScheduleScheduleWizard_1.isScheduleValid)(data, isOneOff),
                renderSummary: () => React.createElement(ScheduleScheduleSummary_1.ScheduleScheduleSummary, null),
                render: () => (React.createElement(rebass_1.Box, { p: 3 },
                    React.createElement(ScheduleScheduleWizard_1.ScheduleScheduleWizard, { isOneOff: isOneOff, onSetIsOneOff: setIsOneOff, onChange: setSchedule }))),
            },
            {
                details: 'Select Schedule Tags',
                title: 'Tags',
                isVisible: (_, api) => api.internalApi.shouldDisplayTagSections(),
                render: () => (React.createElement(rebass_1.Box, { padding: 2 },
                    React.createElement(ObjectTagsWizardSection_1.ObjectTagsWizardSection, { onChange: setSchedule }))),
                renderSummary: ObjectTagsWizardSection_1.renderObjectTagsSummary,
            },
            '-',
            {
                render: () => {
                    return (React.createElement(rebass_1.Box, { padding: 2 },
                        React.createElement(OnePageAdaptableWizard_1.OnePageWizardSummary, null)));
                },
                title: 'Summary',
            },
        ] }));
};
exports.ScheduleWizard = ScheduleWizard;
