import { jsx as _jsx, jsxs as _jsxs, Fragment as _Fragment } from "react/jsx-runtime";
import classNames from 'classnames';
import { useCallback, useImperativeHandle, useMemo, } from 'react';
import { Controller, useForm } from 'react-hook-form';
import { getErrorMessage } from '../../../lib/errors.js';
import { fahrenheitToCelsius } from '../../../lib/seam/thermostats/unit-conversion.js';
import { useCreateThermostatClimatePreset } from '../../../lib/seam/thermostats/use-create-thermostat-climate-preset.js';
import { useUpdateThermostatClimatePreset } from '../../../lib/seam/thermostats/use-update-thermostat-climate-preset.js';
import { Button } from '../../../lib/ui/Button.js';
import { FormField } from '../../../lib/ui/FormField.js';
import { InputLabel } from '../../../lib/ui/InputLabel.js';
import { ContentHeader } from '../../../lib/ui/layout/ContentHeader.js';
import { Snackbar } from '../../../lib/ui/Snackbar/Snackbar.js';
import { TextField } from '../../../lib/ui/TextField/TextField.js';
import { ClimateModeMenu } from '../../../lib/ui/thermostat/ClimateModeMenu.js';
import { FanModeMenu } from '../../../lib/ui/thermostat/FanModeMenu.js';
import { TemperatureControlGroup } from '../../../lib/ui/thermostat/TemperatureControlGroup.js';
export function ClimatePreset(props) {
    const { preset, onBack, device, ...attrs } = props;
    return (_jsxs("div", { ...attrs, className: classNames('seam-thermostat-climate-preset', attrs.className), children: [_jsx(ContentHeader, { title: preset == null ? t.crateNewPreset : preset.display_name, onBack: onBack }), preset == null ? (_jsx(CreateForm, { device: device, onComplete: onBack })) : (_jsx(UpdateForm, { device: device, onComplete: onBack, preset: preset }))] }));
}
function PresetForm(props) {
    const { defaultValues, device, instanceRef, loading, onSubmit, withKeyField, } = props;
    const form = useForm({ defaultValues });
    useImperativeHandle(instanceRef, () => form);
    const { register, handleSubmit, formState: { errors }, watch, setValue, control, } = form;
    const state = watch();
    const onHvacModeChange = (mode) => {
        if (mode === 'heat_cool') {
            setValue('heatPoint', defaultValues.heatPoint);
            setValue('coolPoint', defaultValues.coolPoint);
        }
        else if (mode === 'heat') {
            setValue('heatPoint', defaultValues.heatPoint);
            setValue('coolPoint', undefined);
        }
        else if (mode === 'cool') {
            setValue('heatPoint', undefined);
            setValue('coolPoint', defaultValues.coolPoint);
        }
        else {
            setValue('heatPoint', undefined);
            setValue('coolPoint', undefined);
        }
    };
    const otherClimatePresets = useMemo(() => {
        if (withKeyField !== true)
            return [];
        return (device.properties.available_climate_presets ?? []).filter((other) => other.climate_preset_key !== defaultValues.key);
    }, [defaultValues, device, withKeyField]);
    const onValid = useCallback(() => {
        onSubmit(state);
    }, [onSubmit, state]);
    return (_jsx("div", { className: 'seam-main', children: _jsxs("form", { onSubmit: (e) => {
                void handleSubmit(onValid)(e);
            }, children: [withKeyField === true && (_jsxs(FormField, { children: [_jsx(InputLabel, { children: "Key" }), _jsx(TextField, { size: 'large', clearable: true, hasError: errors.key != null, helperText: errors.key?.message, inputProps: {
                                ...register('key', {
                                    required: 'required',
                                    setValueAs: (value) => value.trim(),
                                    validate(value) {
                                        if (value.includes(' ')) {
                                            return t.keyCannotContainSpaces;
                                        }
                                        const exists = otherClimatePresets.some((other) => other.climate_preset_key === value);
                                        if (exists) {
                                            return t.keyAlreadyExists;
                                        }
                                        return true;
                                    },
                                }),
                            } })] })), _jsxs(FormField, { children: [_jsx(InputLabel, { children: t.nameField }), _jsx(TextField, { size: 'large', clearable: true, hasError: errors.name != null, helperText: errors.name?.message, inputProps: register('name', {
                                required: false,
                                setValueAs: (value) => value.trim(),
                            }) })] }), state.fanMode != null && (_jsxs(FormField, { children: [_jsx(InputLabel, { children: t.fanModeField }), _jsx(Controller, { control: control, name: 'fanMode', render: ({ field: { onChange, value } }) => value != null ? (_jsx(FanModeMenu, { block: true, size: 'large', mode: value, onChange: onChange })) : (_jsx(_Fragment, {})) })] })), state.hvacMode != null && (_jsxs(FormField, { children: [_jsx(InputLabel, { children: t.hvacModeField }), _jsx(Controller, { control: control, name: 'hvacMode', render: ({ field: { onChange, value } }) => value == null ? (_jsx(_Fragment, {})) : (_jsx(ClimateModeMenu, { block: true, size: 'large', buttonTextVisible: true, mode: value, onChange: (value) => {
                                    onHvacModeChange(value);
                                    onChange(value);
                                } })) })] })), state.hvacMode !== 'off' && state.hvacMode != null && (_jsxs(FormField, { children: [_jsx(InputLabel, { children: t.heatCoolField }), _jsx(TemperatureControlGroup, { mode: state.hvacMode, onHeatValueChange: (value) => {
                                setValue('heatPoint', value);
                            }, onCoolValueChange: (value) => {
                                setValue('coolPoint', value);
                            }, heatValue: state.heatPoint ?? 0, coolValue: state.coolPoint ?? 0, minHeat: device.properties.min_heating_cooling_delta_fahrenheit, maxHeat: device.properties.max_heating_set_point_fahrenheit, minCool: device.properties.min_cooling_set_point_fahrenheit, maxCool: device.properties.max_cooling_set_point_fahrenheit, delta: device.properties.min_heating_cooling_delta_fahrenheit })] })), _jsx("div", { className: 'seam-climate-preset-buttons', children: _jsx(Button, { type: 'submit', variant: 'solid', disabled: loading, loading: loading, children: t.save }) })] }) }));
}
function CreateForm({ device, onComplete }) {
    const { mutate, isError, error, isPending } = useCreateThermostatClimatePreset();
    const errorMessage = getErrorMessage(error);
    const onSubmit = useCallback((values) => {
        mutate({
            climate_preset_key: values.key,
            device_id: device.device_id,
            name: values.name === '' ? undefined : values.name,
            cooling_set_point_fahrenheit: values.coolPoint,
            heating_set_point_fahrenheit: values.heatPoint,
            fan_mode_setting: values.fanMode,
            cooling_set_point_celsius: fahrenheitToCelsius(values.coolPoint),
            heating_set_point_celsius: fahrenheitToCelsius(values.heatPoint),
            hvac_mode_setting: values.hvacMode,
        }, { onSuccess: onComplete });
    }, [device, mutate, onComplete]);
    return (_jsxs(_Fragment, { children: [_jsx(Snackbar, { message: errorMessage, variant: 'error', visible: isError, automaticVisibility: true }), _jsx(PresetForm, { defaultValues: {
                    key: '',
                    coolPoint: 60,
                    heatPoint: 80,
                    name: '',
                    hvacMode: 'off',
                    fanMode: 'auto',
                }, device: device, loading: isPending, onSubmit: onSubmit, withKeyField: true })] }));
}
function UpdateForm({ device, onComplete, preset, }) {
    const { mutate, isError, error, isPending } = useUpdateThermostatClimatePreset();
    const defaultValues = useMemo(() => ({
        coolPoint: preset.cooling_set_point_fahrenheit,
        heatPoint: preset.heating_set_point_fahrenheit,
        name: preset.display_name,
        hvacMode: preset.hvac_mode_setting,
        fanMode: preset.fan_mode_setting,
        key: preset.climate_preset_key,
    }), [preset]);
    const onSubmit = useCallback((values) => {
        mutate({
            climate_preset_key: values.key,
            device_id: device.device_id,
            name: values.name === '' ? undefined : values.name,
            cooling_set_point_fahrenheit: values.coolPoint,
            heating_set_point_fahrenheit: values.heatPoint,
            fan_mode_setting: values.fanMode,
            cooling_set_point_celsius: fahrenheitToCelsius(values.coolPoint),
            heating_set_point_celsius: fahrenheitToCelsius(values.heatPoint),
            hvac_mode_setting: values.hvacMode,
            manual_override_allowed: false, // deprecated but type still wants it.
        }, { onSuccess: onComplete });
    }, [device, mutate, onComplete]);
    const errorMessage = getErrorMessage(error);
    return (_jsxs(_Fragment, { children: [_jsx(Snackbar, { message: errorMessage, variant: 'error', visible: isError, automaticVisibility: true }), _jsx(PresetForm, { defaultValues: defaultValues, device: device, loading: isPending, onSubmit: onSubmit })] }));
}
const t = {
    keyAlreadyExists: 'Climate Preset with this key already exists.',
    keyCannotContainSpaces: 'Climate Preset key cannot contain spaces.',
    nameField: 'Display Name (Optional)',
    fanModeField: 'Fan Mode',
    hvacModeField: 'HVAC Mode',
    heatCoolField: 'Heat / Cool',
    delete: 'Delete',
    save: 'Save',
    crateNewPreset: 'Create New Climate Preset',
    unknownErrorOccured: 'An unknown error occurred.',
};
//# sourceMappingURL=ClimatePreset.js.map