'use strict';

const domContextHooks_module = require('./dom-context-hooks.module-e7b6727c.js');
const index_module = require('./index.module-f8c8495f.js');
const jsonpointer = require('./jsonpointer-11327262.js');
const useRegistrationFormState = require('./useRegistrationFormState-4fe5b770.js');

function useInstantAccessRegistration(props) {
  const locale = index_module.L();
  const cookie = new URLSearchParams(window.location.search);
  const cookies = cookie === null || cookie === void 0 ? void 0 : cookie.get("_saasquatch");
  const { registrationFormState, setRegistrationFormState } = useRegistrationFormState.useRegistrationFormState();
  const [request, { loading, errors, data }] = index_module.vn();
  const [error, setError] = domContextHooks_module.useState("");
  const submit = async (event) => {
    var _a;
    setError("");
    let formControls = event.target.getFormControls();
    let formData = event.detail.formData;
    let validationErrors = {};
    formControls === null || formControls === void 0 ? void 0 : formControls.map((control) => {
      if (!control.name)
        return;
      const key = control.name;
      const value = control.value;
      jsonpointer.jsonpointer.set(formData, key, value);
      if (control.required && !value) {
        jsonpointer.jsonpointer.set(validationErrors, key, props.requiredFieldErrorMessage);
      }
      // custom validation
      if (typeof control.validationError === "function") {
        const validate = control.validationError;
        const validationError = validate({ control, key, value });
        if (validationError)
          jsonpointer.jsonpointer.set(validationErrors, key, validationError);
      }
    });
    const variables = {
      firstName: formData.firstName,
      lastName: formData.lastName,
      email: formData.email,
      locale,
      ...((props === null || props === void 0 ? void 0 : props.includeCookies) && cookies ? { cookies } : {}),
    };
    try {
      const result = await request(variables);
      if (result instanceof Error) {
        throw result;
      }
      setRegistrationFormState({
        ...registrationFormState,
        loading: false,
        error: "",
        validationErrors: {},
      });
    }
    catch (error) {
      // check for invalid email
      if ((_a = error === null || error === void 0 ? void 0 : error.message) === null || _a === void 0 ? void 0 : _a.includes("is not valid")) {
        setRegistrationFormState({
          ...registrationFormState,
          loading: false,
          error: "",
          validationErrors: { email: props.invalidEmailErrorMessage },
        });
      }
      else if ((error === null || error === void 0 ? void 0 : error.name) === "fraud_error") {
        setRegistrationFormState({
          ...registrationFormState,
          loading: false,
          error: "FRAUD",
        });
      }
      else {
        setRegistrationFormState({
          ...registrationFormState,
          loading: false,
          error: props.networkErrorMessage,
          validationErrors: {},
        });
      }
    }
  };
  return {
    states: {
      loading,
      error: registrationFormState === null || registrationFormState === void 0 ? void 0 : registrationFormState.error,
      registrationFormState,
    },
    callbacks: {
      submit,
    },
  };
}

exports.useInstantAccessRegistration = useInstantAccessRegistration;
