import { jsx as _jsx } from "react/jsx-runtime";
import { useState } from 'react';
import { useComponentTelemetry } from '../../../../lib/telemetry/index.js';
import { createIsoDate } from '../../../../lib/dates.js';
import { useAccessCode, } from '../../../../lib/seam/access-codes/use-access-code.js';
import { useUpdateAccessCode } from '../../../../lib/seam/access-codes/use-update-access-code.js';
import { withRequiredCommonProps, } from '../../../../lib/seam/components/common-props.js';
import { useDevice } from '../../../../lib/seam/devices/use-device.js';
import { getValidationError } from '../../../../lib/seam/error-handlers.js';
import { AccessCodeForm, } from '../../../../lib/ui/AccessCodeForm/AccessCodeForm.js';
export const NestedEditAccessCodeForm = withRequiredCommonProps(EditAccessCodeForm);
export function EditAccessCodeForm({ accessCodeId, onBack, className, }) {
    useComponentTelemetry('EditAccessCodeForm');
    const { accessCode } = useAccessCode({
        access_code_id: accessCodeId,
    });
    if (accessCode == null) {
        return null;
    }
    return (_jsx(Content, { accessCode: accessCode, className: className, onBack: onBack }));
}
function Content({ className, onBack, accessCode, }) {
    const { device } = useDevice({
        device_id: accessCode.device_id,
    });
    const { submit, isSubmitting, codeError } = useSubmitEditAccessCode(accessCode, onBack);
    if (device == null) {
        return null;
    }
    return (_jsx(AccessCodeForm, { onBack: onBack, className: className, accessCode: accessCode, device: device, onSubmit: submit, isSubmitting: isSubmitting, codeError: codeError }));
}
function useSubmitEditAccessCode(accessCode, onSuccess) {
    const { mutate, isLoading: isSubmitting } = useUpdateAccessCode();
    const [codeError, setCodeError] = useState(null);
    const handleError = (error) => {
        const codeError = getValidationError({ error, property: 'code' });
        if (codeError != null) {
            setCodeError(codeError);
        }
    };
    const submit = (data) => {
        setCodeError(null);
        const { name, code, type, device, startDate, endDate, timezone } = data;
        if (name === '') {
            return;
        }
        if (isSubmitting) {
            return;
        }
        if (type === 'time_bound') {
            mutate({
                access_code_id: accessCode.access_code_id,
                name,
                code,
                device_id: device.device_id,
                type: 'time_bound',
                starts_at: createIsoDate(startDate, timezone),
                ends_at: createIsoDate(endDate, timezone),
            }, {
                onSuccess,
                onError: handleError,
            });
            return;
        }
        mutate({
            access_code_id: accessCode.access_code_id,
            name,
            type: 'ongoing',
            device_id: device.device_id,
        }, {
            onSuccess,
            onError: handleError,
        });
    };
    return { submit, isSubmitting, codeError };
}
//# sourceMappingURL=EditAccessCodeForm.js.map