"use strict";Object.defineProperty(exports, "__esModule", {value: true}); function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }

var _chunkQBVSY6U2js = require('./chunk-QBVSY6U2.js');


var _chunkDUDWMC5Pjs = require('./chunk-DUDWMC5P.js');


var _chunkN2FBU773js = require('./chunk-N2FBU773.js');

// tmp-babel/profiles/web/profile-popover/AddProfileMenuItem/CreateProfileModal/index.js
var _icons = require('@baseapp-frontend/design-system/components/web/icons');
var _inputs = require('@baseapp-frontend/design-system/components/web/inputs');
var _utils = require('@baseapp-frontend/utils');
var _zod = require('@hookform/resolvers/zod');
var _lab = require('@mui/lab');
var _material = require('@mui/material');
var _reacthookform = require('react-hook-form');
var _reactrelay = require('react-relay');
var _slugify = require('slugify'); var _slugify2 = _interopRequireDefault(_slugify);
var _jsxruntime = require('react/jsx-runtime');
function _slicedToArray(r, e) {
  return _arrayWithHoles(r) || _iterableToArrayLimit(r, e) || _unsupportedIterableToArray(r, e) || _nonIterableRest();
}
function _nonIterableRest() {
  throw new TypeError("Invalid attempt to destructure non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method.");
}
function _unsupportedIterableToArray(r, a) {
  if (r) {
    if ("string" == typeof r) return _arrayLikeToArray(r, a);
    var t = {}.toString.call(r).slice(8, -1);
    return "Object" === t && r.constructor && (t = r.constructor.name), "Map" === t || "Set" === t ? Array.from(r) : "Arguments" === t || /^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(t) ? _arrayLikeToArray(r, a) : void 0;
  }
}
function _arrayLikeToArray(r, a) {
  (null == a || a > r.length) && (a = r.length);
  for (var e = 0, n = Array(a); e < a; e++) n[e] = r[e];
  return n;
}
function _iterableToArrayLimit(r, l) {
  var t = null == r ? null : "undefined" != typeof Symbol && r[Symbol.iterator] || r["@@iterator"];
  if (null != t) {
    var e, n, i, u, a = [], f = true, o = false;
    try {
      if (i = (t = t.call(r)).next, 0 === l) {
        if (Object(t) !== t) return;
        f = false;
      } else for (; !(f = (e = i.call(t)).done) && (a.push(e.value), a.length !== l); f = true) ;
    } catch (r2) {
      o = true, n = r2;
    } finally {
      try {
        if (!f && null != t["return"] && (u = t["return"](), Object(u) !== u)) return;
      } finally {
        if (o) throw n;
      }
    }
    return a;
  }
}
function _arrayWithHoles(r) {
  if (Array.isArray(r)) return r;
}
var nextClientMutationId = 0;
var CreateProfileModal = function CreateProfileModal2(_ref) {
  var _ref$addNewProfileLab = _ref.addNewProfileLabel, addNewProfileLabel = _ref$addNewProfileLab === void 0 ? "New organization" : _ref$addNewProfileLab, _ref$termsAndConditio = _ref.termsAndConditionsUrl, termsAndConditionsUrl = _ref$termsAndConditio === void 0 ? "" : _ref$termsAndConditio, _ref$addNewProfileDes = _ref.addNewProfileDescription, addNewProfileDescription = _ref$addNewProfileDes === void 0 ? "Create an organization and invite multiple members to manage and collaborate." : _ref$addNewProfileDes, _ref$submitLabel = _ref.submitLabel, submitLabel = _ref$submitLabel === void 0 ? "Create Organization" : _ref$submitLabel, onClose = _ref.onClose, open = _ref.open, setOpen = _ref.setOpen, userId = _ref.userId;
  var form = _reacthookform.useForm.call(void 0, {
    mode: "onChange",
    defaultValues: {
      name: "",
      urlPath: ""
    },
    resolver: _zod.zodResolver.call(void 0, _chunkQBVSY6U2js.schema)
  });
  var _useOrganizationCreat = _chunkN2FBU773js.useOrganizationCreateMutation.call(void 0, ), _useOrganizationCreat2 = _slicedToArray(_useOrganizationCreat, 2), commitMutation = _useOrganizationCreat2[0], isMutationInFlight = _useOrganizationCreat2[1];
  var handleClose = function handleClose2() {
    setOpen(false);
    form.reset();
    onClose === null || onClose === void 0 || onClose();
  };
  var onSubmit = function onSubmit2(data) {
    if (isMutationInFlight) return;
    nextClientMutationId += 1;
    var clientMutationId = nextClientMutationId.toString();
    var connections = _reactrelay.ConnectionHandler.getConnectionID(userId, "ProfilesListFragment_profiles");
    commitMutation({
      variables: {
        input: {
          name: data.name,
          urlPath: data.urlPath,
          clientMutationId
        },
        connections: [connections]
      },
      onCompleted: function onCompleted(response, errors) {
        var _response$organizatio;
        if (errors) {
          console.error(errors);
          return;
        }
        var mutationErrors = response === null || response === void 0 || (_response$organizatio = response.organizationCreate) === null || _response$organizatio === void 0 ? void 0 : _response$organizatio.errors;
        _utils.setFormRelayErrors.call(void 0, form, mutationErrors);
        if (!(mutationErrors !== null && mutationErrors !== void 0 && mutationErrors.length)) {
          handleClose();
        }
      },
      onError: console.error
    });
  };
  return /* @__PURE__ */ _jsxruntime.jsxs.call(void 0, _material.Dialog, {
    open,
    onClose: handleClose,
    "aria-labelledby": "organization-modal-title",
    "aria-describedby": "organization-modal-description",
    PaperProps: {
      sx: {
        borderRadius: {
          xs: 0,
          sm: 2
        },
        minWidth: {
          xs: "100%",
          sm: 0
        },
        margin: {
          xs: 0,
          sm: 2
        },
        maxWidth: {
          sm: 366
        },
        height: {
          xs: "100%",
          sm: "auto"
        },
        minHeight: {
          xs: "100%",
          sm: "auto"
        }
      }
    },
    children: [/* @__PURE__ */ _jsxruntime.jsx.call(void 0, _material.DialogTitle, {
      id: "organization-modal-title",
      children: /* @__PURE__ */ _jsxruntime.jsxs.call(void 0, _material.Box, {
        sx: {
          display: "flex",
          alignItems: "center",
          gap: 2,
          justifyContent: "space-between"
        },
        children: [/* @__PURE__ */ _jsxruntime.jsx.call(void 0, _material.Typography, {
          variant: "h6",
          children: addNewProfileLabel
        }), /* @__PURE__ */ _jsxruntime.jsx.call(void 0, _material.IconButton, {
          onClick: handleClose,
          children: /* @__PURE__ */ _jsxruntime.jsx.call(void 0, _icons.CloseIcon, {})
        })]
      })
    }), /* @__PURE__ */ _jsxruntime.jsxs.call(void 0, _chunkDUDWMC5Pjs.Form, {
      onSubmit: form.handleSubmit(onSubmit),
      children: [/* @__PURE__ */ _jsxruntime.jsxs.call(void 0, _material.DialogContent, {
        sx: {
          display: "flex",
          flexDirection: "column",
          gap: 4
        },
        id: "organization-modal-description",
        children: [/* @__PURE__ */ _jsxruntime.jsx.call(void 0, _material.Typography, {
          color: "text.secondary",
          children: addNewProfileDescription
        }), /* @__PURE__ */ _jsxruntime.jsxs.call(void 0, _material.Box, {
          display: "flex",
          flexDirection: "column",
          gap: 2,
          children: [/* @__PURE__ */ _jsxruntime.jsx.call(void 0, _inputs.TextField, {
            label: "Name",
            name: "name",
            size: "medium",
            control: form.control,
            onBlur: function onBlur() {
              if (!form.getValues("urlPath")) {
                form.setValue("urlPath", _slugify2.default.call(void 0, form.getValues("name").toLocaleLowerCase()));
                form.trigger("urlPath");
              }
            }
          }), /* @__PURE__ */ _jsxruntime.jsx.call(void 0, _inputs.TextField, {
            label: "URL Path",
            name: "urlPath",
            size: "medium",
            control: form.control
          }), /* @__PURE__ */ _jsxruntime.jsxs.call(void 0, _material.Typography, {
            color: "text.primary",
            children: ["Upon confirming, you agree to our", " ", /* @__PURE__ */ _jsxruntime.jsx.call(void 0, _material.Link, {
              display: "inline",
              href: termsAndConditionsUrl,
              target: "_blank",
              children: "Terms and Conditions."
            })]
          })]
        })]
      }), /* @__PURE__ */ _jsxruntime.jsxs.call(void 0, _material.DialogActions, {
        sx: {
          justifyContent: "flex-end"
        },
        children: [/* @__PURE__ */ _jsxruntime.jsx.call(void 0, _material.Button, {
          variant: "outlined",
          color: "inherit",
          onClick: handleClose,
          sx: {
            width: "auto"
          },
          children: "Cancel"
        }), /* @__PURE__ */ _jsxruntime.jsx.call(void 0, _lab.LoadingButton, {
          variant: "contained",
          color: "inherit",
          sx: {
            width: "auto"
          },
          type: "submit",
          loading: isMutationInFlight,
          disabled: isMutationInFlight || Object.keys(form.formState.errors).length > 0 || !form.formState.isValid,
          children: submitLabel
        })]
      })]
    })]
  });
};
var CreateProfileModal_default = CreateProfileModal;



exports.CreateProfileModal_default = CreateProfileModal_default;
//# sourceMappingURL=chunk-5KYTNKYZ.js.map