import { jsx as _jsx } from "react/jsx-runtime";
import { isSeamHttpInvalidInputError, } from '@seamapi/http/connect';
import { useState } from 'react';
import { shake } from '../../../../lib/object.js';
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 { 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, isPending: 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) => {
        if (isSeamHttpInvalidInputError(error)) {
            const errors = shake({
                code: error.getValidationErrorMessages('code')[0],
                name: error.getValidationErrorMessages('name')[0],
            });
            if (Object.keys(errors).length === 0) {
                setResponseErrors({
                    unknown: error.message,
                });
            }
        }
        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