import { jsx as _jsx, Fragment as _Fragment } from "react/jsx-runtime";
import classNames from 'classnames';
import { useEffect, useState } from 'react';
import { useForm } from 'react-hook-form';
import { isThermostatDevice, } from 'seamapi';
import { getSystemTimeZone } from '../../../lib/dates.js';
import { useSeamClient } from '../../../lib/index.js';
import { ClimateSettingScheduleFormClimateSetting } from '../../../lib/ui/ClimateSettingForm/ClimateSettingScheduleFormClimateSetting.js';
import { ClimateSettingScheduleFormDefaultClimateSetting } from '../../../lib/ui/ClimateSettingForm/ClimateSettingScheduleFormDefaultClimateSetting.js';
import { ClimateSettingScheduleFormDeviceSelect } from '../../../lib/ui/ClimateSettingForm/ClimateSettingScheduleFormDeviceSelect.js';
import { ClimateSettingScheduleFormNameAndSchedule } from '../../../lib/ui/ClimateSettingForm/ClimateSettingScheduleFormNameAndSchedule.js';
import { ClimateSettingScheduleFormTimeZonePicker } from '../../../lib/ui/ClimateSettingForm/ClimateSettingScheduleFormTimeZonePicker.js';
export function ClimateSettingScheduleForm({ className, ...props }) {
    return (_jsx("div", { className: classNames('seam-climate-setting-schedule-form', className), children: _jsx(Content, { ...props }) }));
}
function Content({ onBack, }) {
    const { client } = useSeamClient();
    const { control, watch, resetField } = useForm({
        defaultValues: {
            deviceId: '',
            name: '',
            startDate: '',
            endDate: '',
            timeZone: getSystemTimeZone(),
            climateSetting: {
                hvacModeSetting: 'heat_cool',
                heatingSetPoint: 70,
                coolingSetPoint: 75,
            },
        },
    });
    const deviceId = watch('deviceId');
    const timeZone = watch('timeZone');
    const [page, setPage] = useState('device_select');
    const returnToDeviceSelect = () => {
        resetField('deviceId');
        setPage('device_select');
    };
    useEffect(() => {
        if (page === 'device_select' && deviceId !== '' && client != null) {
            client.devices
                .get({ device_id: deviceId })
                .then((device) => {
                if (!isThermostatDevice(device))
                    return;
                if (device.properties.default_climate_setting != null) {
                    setPage('name_and_schedule');
                    return;
                }
                setPage('default_setting');
            })
                .catch(() => { });
        }
    }, [client, deviceId, page, setPage]);
    if (page === 'device_select') {
        return (_jsx(ClimateSettingScheduleFormDeviceSelect, { title: t.addNewClimateSettingSchedule, control: control, onBack: onBack }));
    }
    if (page === 'default_setting') {
        return (_jsx(ClimateSettingScheduleFormDefaultClimateSetting, { title: t.addNewClimateSettingSchedule, deviceId: deviceId, onBack: returnToDeviceSelect, onCancel: onBack, onSave: () => {
                setPage('name_and_schedule');
            } }));
    }
    if (page === 'name_and_schedule') {
        return (_jsx(ClimateSettingScheduleFormNameAndSchedule, { title: t.addNewClimateSettingSchedule, control: control, deviceId: deviceId, onBack: returnToDeviceSelect, onCancel: onBack, onNext: () => {
                setPage('climate_setting');
            }, onChangeTimeZone: () => {
                setPage('time_zone_select');
            }, timeZone: timeZone }));
    }
    if (page === 'time_zone_select') {
        return (_jsx(ClimateSettingScheduleFormTimeZonePicker, { control: control, onClose: () => {
                setPage('name_and_schedule');
            } }));
    }
    if (page === 'climate_setting') {
        return (_jsx(ClimateSettingScheduleFormClimateSetting, { title: t.addNewClimateSettingSchedule, control: control, deviceId: deviceId, onBack: () => {
                setPage('name_and_schedule');
            }, onCancel: onBack, onSave: () => { } }));
    }
    return _jsx(_Fragment, {});
}
export const t = {
    addNewClimateSettingSchedule: 'Add a climate setting schedule',
};
//# sourceMappingURL=ClimateSettingScheduleForm.js.map