import { jsx as _jsx } from "react/jsx-runtime";
import { useState } from 'react';
import { useCreateAccessCode } from '../../../../lib/seam/access-codes/use-create-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 { useComponentTelemetry } from '../../../../lib/telemetry/index.js';
import { AccessCodeForm, } from '../../../../lib/ui/AccessCodeForm/AccessCodeForm.js';
export const NestedCreateAccessCodeForm = withRequiredCommonProps(CreateAccessCodeForm);
export function CreateAccessCodeForm({ className, onBack, deviceId, onSuccess, }) {
    useComponentTelemetry('CreateAccessCodeForm');
    const { device } = useDevice({
        device_id: deviceId,
    });
    if (device == null) {
        return null;
    }
    return (_jsx(Content, { device: device, className: className, onBack: onBack, onSuccess: onSuccess }));
}
function Content({ device, className, onBack, onSuccess, }) {
    const { submit, isSubmitting, responseErrors } = useSubmitCreateAccessCode({
        onSuccess: (accessCode) => {
            if (onSuccess != null) {
                onSuccess(accessCode.access_code_id);
            }
            if (onBack != null) {
                onBack();
            }
        },
    });
    return (_jsx(AccessCodeForm, { device: device, className: className, onBack: onBack, onSubmit: submit, isSubmitting: isSubmitting, responseErrors: responseErrors }));
}
function useSubmitCreateAccessCode(params) {
    const { onSuccess } = params;
    const { mutate, isLoading: isSubmitting } = useCreateAccessCode();
    const { responseErrors, handleResponseError, resetResponseErrors } = useResponseErrors();
    const submit = (data) => {
        resetResponseErrors();
        const { name, code, type, device, startDate, endDate } = data;
        if (name === '') {
            return;
        }
        if (isSubmitting) {
            return;
        }
        if (type === 'time_bound') {
            mutate({
                name,
                code,
                device_id: device.device_id,
                starts_at: startDate,
                ends_at: endDate,
            }, {
                onSuccess,
                onError: handleResponseError,
            });
            return;
        }
        mutate({
            name,
            code,
            device_id: device.device_id,
        }, {
            onSuccess,
            onError: handleResponseError,
        });
    };
    return { submit, isSubmitting, responseErrors };
}
export function useResponseErrors() {
    const [responseErrors, setResponseErrors] = useState(null);
    const handleResponseError = (error) => {
        const code = getValidationError({ error, property: 'code' });
        const name = getValidationError({ error, property: 'name' });
        if (code != null || name != null) {
            setResponseErrors({
                code,
                name,
            });
            return;
        }
        setResponseErrors({
            unknown: t.genericResponseError,
        });
    };
    const resetResponseErrors = () => {
        setResponseErrors(null);
    };
    return {
        responseErrors,
        handleResponseError,
        resetResponseErrors,
    };
}
const t = {
    genericResponseError: 'The code could not be saved. Please try again.',
};
//# sourceMappingURL=CreateAccessCodeForm.js.map