'use strict';

Object.defineProperty(exports, '__esModule', { value: true });

const index = require('./index-8277cbb3.js');
require('./global-b7b1b8fe.js');
const domContextHooks_module = require('./dom-context-hooks.module-e7b6727c.js');
const index_module = require('./index.module-f8c8495f.js');
const cjs = require('./cjs-1066ec21.js');
const utils = require('./utils-6847bc06.js');
require('./JSS-8503a151.js');
require('./sqm-text-span-view-b9e10f5d.js');
const sqmPortalProfileView = require('./sqm-portal-profile-view-3515e1e5.js');
const utilities = require('./utilities-fcdb9504.js');

const GET_USER = index_module.dist.gql `
  query {
    viewer {
      ... on User {
        id
        accountId
        firstName
        lastName
        email
        countryCode
      }
    }
  }
`;
const UPSERT_USER = index_module.dist.gql `
  mutation (
    $id: String!
    $accountId: String!
    $firstName: String!
    $lastName: String!
  ) {
    upsertUser(
      userInput: {
        id: $id
        accountId: $accountId
        firstName: $firstName
        lastName: $lastName
      }
    ) {
      firstName
      lastName
    }
  }
`;
// view doesn't tolerate undefined, even when we're loading
const defaultFormState = {
  country: "",
  email: "",
  firstName: "",
  lastName: "",
  errors: {},
  error: "",
};
function usePortalProfile(props) {
  const userIdent = index_module.J();
  const [success, setSuccess] = domContextHooks_module.useState(false);
  const [userData, setUserData] = domContextHooks_module.useState(undefined);
  const [formState, setFormState] = domContextHooks_module.useState(defaultFormState);
  const userDataResponse = index_module.wn(GET_USER, {}, !(userIdent === null || userIdent === void 0 ? void 0 : userIdent.jwt));
  const [upsertUser, upsertUserResponse] = index_module.$e(UPSERT_USER);
  domContextHooks_module.useEffect(() => {
    if (upsertUserResponse === null || upsertUserResponse === void 0 ? void 0 : upsertUserResponse.loading)
      return;
    if (upsertUserResponse === null || upsertUserResponse === void 0 ? void 0 : upsertUserResponse.errors) {
      setFormState((state) => {
        var _a, _b, _c;
        return ({
          ...state,
          error: (_c = (_b = (_a = upsertUserResponse === null || upsertUserResponse === void 0 ? void 0 : upsertUserResponse.errors) === null || _a === void 0 ? void 0 : _a.response) === null || _b === void 0 ? void 0 : _b.errors) === null || _c === void 0 ? void 0 : _c[0].message,
        });
      });
    }
    else {
      setUserData((state) => {
        var _a;
        return ({
          ...state,
          ...(_a = upsertUserResponse === null || upsertUserResponse === void 0 ? void 0 : upsertUserResponse.data) === null || _a === void 0 ? void 0 : _a.upsertUser,
        });
      });
      if (upsertUserResponse === null || upsertUserResponse === void 0 ? void 0 : upsertUserResponse.data)
        setSuccess(true);
    }
  }, [upsertUserResponse === null || upsertUserResponse === void 0 ? void 0 : upsertUserResponse.loading]);
  domContextHooks_module.useEffect(() => {
    var _a, _b;
    setUserData((data) => { var _a; return ({ ...data, ...(_a = userDataResponse === null || userDataResponse === void 0 ? void 0 : userDataResponse.data) === null || _a === void 0 ? void 0 : _a.viewer }); });
    setFormState({
      ...defaultFormState,
      ...(_a = userDataResponse.data) === null || _a === void 0 ? void 0 : _a.viewer,
      ...(_b = upsertUserResponse.data) === null || _b === void 0 ? void 0 : _b.upsertUser,
    });
  }, [userDataResponse === null || userDataResponse === void 0 ? void 0 : userDataResponse.data]);
  domContextHooks_module.useEffect(() => {
    var _a, _b;
    if ((_a = upsertUserResponse === null || upsertUserResponse === void 0 ? void 0 : upsertUserResponse.errors) === null || _a === void 0 ? void 0 : _a.message) {
      setFormState({
        ...(_b = userDataResponse.data) === null || _b === void 0 ? void 0 : _b.viewer,
        error: props.networkRequestMessage,
      });
    }
  }, [upsertUserResponse === null || upsertUserResponse === void 0 ? void 0 : upsertUserResponse.errors]);
  const onSubmit = () => {
    setSuccess(false);
    if (formState.firstName && formState.lastName) {
      upsertUser({
        id: userIdent === null || userIdent === void 0 ? void 0 : userIdent.id,
        accountId: userIdent === null || userIdent === void 0 ? void 0 : userIdent.accountId,
        firstName: formState.firstName,
        lastName: formState.lastName,
      });
      setFormState((s) => ({ ...s, errors: {}, error: "" }));
      return;
    }
    const errors = {};
    if (!formState.firstName) {
      errors["firstName"] = { message: props.fieldEmptyText };
    }
    if (!formState.lastName) {
      errors["lastName"] = { message: props.fieldEmptyText };
    }
    if (!utilities.isEmpty(errors)) {
      setFormState((e) => ({
        ...e,
        error: props.formErrorText,
      }));
    }
    setFormState((e) => ({ ...e, errors }));
  };
  return {
    states: {
      success,
      loading: (userDataResponse === null || userDataResponse === void 0 ? void 0 : userDataResponse.loading) || upsertUserResponse.loading,
      submitDisabled: false,
      showCountry: !props.hideCountry,
      formState,
      user: userData,
      text: {
        firstnametext: props.firstnametext,
        lastnametext: props.lastnametext,
        countrytext: props.countrytext,
        emailtext: props.emailtext,
        editProfileHeader: props.editProfileHeader,
        editProfileSubHeader: props.editProfileSubHeader,
        submitChangeButtonText: props.submitChangeButtonText,
      },
    },
    callbacks: {
      onSubmit,
      onChange: (e) => {
        const { name, value } = e.target;
        setFormState((data) => ({ ...data, [name]: value }));
      },
    },
  };
}

const PortalProfile = class {
  constructor(hostRef) {
    index.registerInstance(this, hostRef);
    this.ignored = true;
    /**
     * @uiName First name label
     */
    this.firstnametext = "First Name";
    /**
     * @uiName Last name field label
     */
    this.lastnametext = "Last Name";
    /**
     * @uiName Email field label
     */
    this.emailtext = "Email";
    /**
     * @uiName Country field label
     */
    this.countrytext = "Country";
    /**
     * @uiName Edit profile header
     */
    this.editProfileHeader = "Edit your profile";
    /**
     * @uiName Edit profile sub header
     */
    this.editProfileSubHeader = "Personal Information";
    /**
     * @uiName Submit changes button text
     */
    this.submitChangeButtonText = "Submit Changes";
    /**
     * @uiName Hide country field
     */
    this.hideCountry = false;
    /**
     * @uiName Network request error message
     * Displayed when the page fails to load due to a network error. The participant can try refreshing the page.
     */
    this.networkRequestMessage = "An error occurred while loading this page. Please refresh the page.";
    /**
     * @uiName Field empty text
     */
    this.fieldEmptyText = "Field is required";
    /**
     * @uiName Form error message
     */
    this.formErrorText = "Please correct the errors below to update your profile.";
    /**
     * @uiName Submission success text
     */
    this.submissionSuccessText = "Your profile has been successfully updated.";
    domContextHooks_module.h$1(this);
  }
  disconnectedCallback() { }
  render() {
    const props = index_module.isDemo()
      ? usePortalProfileDemo(utils.getProps(this))
      : usePortalProfile(utils.getProps(this));
    return index.h(sqmPortalProfileView.PortalProfileView, Object.assign({}, props));
  }
};
function usePortalProfileDemo(props) {
  return cjs.cjs({
    states: {
      success: false,
      loading: false,
      submitDisabled: false,
      showCountry: !props.hideCountry,
      user: {
        id: "01",
        accountId: "111100000",
        firstName: "Joe",
        lastName: "Smith",
        email: "jsmith@gmail.com",
        countryCode: "CA",
      },
      text: {
        firstnametext: props.firstnametext,
        lastnametext: props.lastnametext,
        emailtext: props.emailtext,
        countrytext: props.countrytext,
        editProfileHeader: props.editProfileHeader,
        editProfileSubHeader: props.editProfileSubHeader,
        submitChangeButtonText: props.submitChangeButtonText,
        submissionSuccessText: props.submissionSuccessText,
        fieldEmptyText: props.fieldEmptyText,
      },
      formState: {
        country: "Canada",
        firstName: "Joe",
        lastName: "Smith",
        errors: null,
        error: "",
      },
    },
    callbacks: {
      onSubmit: (e) => console.log(e),
      onChange: (e) => console.log(e),
    },
  }, props.demoData || {}, { arrayMerge: (_, a) => a });
}

exports.sqm_portal_profile = PortalProfile;
