"use strict";Object.defineProperty(exports, "__esModule", {value: true});

var _chunkA4GGIRWMjs = require('./chunk-A4GGIRWM.js');


var _chunkDHYUUR3Wjs = require('./chunk-DHYUUR3W.js');


var _chunkKY3OAM4Ejs = require('./chunk-KY3OAM4E.js');


var _chunkWNUWPU4Yjs = require('./chunk-WNUWPU4Y.js');


var _chunkNSD4MMXMjs = require('./chunk-NSD4MMXM.js');



var _chunkWJSOX6WAjs = require('./chunk-WJSOX6WA.js');


var _chunkNWQ2TTZQjs = require('./chunk-NWQ2TTZQ.js');


var _chunkPQMT7VJNjs = require('./chunk-PQMT7VJN.js');


var _chunkQRKOXN2Fjs = require('./chunk-QRKOXN2F.js');


var _chunkM4XNAJAZjs = require('./chunk-M4XNAJAZ.js');


var _chunk22ZA7RAXjs = require('./chunk-22ZA7RAX.js');


var _chunkECBMCATXjs = require('./chunk-ECBMCATX.js');

// tmp-babel/messages/web/GroupChatEdit/index.js
var _react = require('react');
var _authentication = require('@baseapp-frontend/authentication');
var _web = require('@baseapp-frontend/design-system/hooks/web');
var _utils = require('@baseapp-frontend/utils');
var _zod = require('@hookform/resolvers/zod');
var _material = require('@mui/material');
var _reacthookform = require('react-hook-form');
var _reactrelay = require('react-relay');
var _jsxruntime = require('react/jsx-runtime');
function _typeof(o) {
  "@babel/helpers - typeof";
  return _typeof = "function" == typeof Symbol && "symbol" == typeof Symbol.iterator ? function(o2) {
    return typeof o2;
  } : function(o2) {
    return o2 && "function" == typeof Symbol && o2.constructor === Symbol && o2 !== Symbol.prototype ? "symbol" : typeof o2;
  }, _typeof(o);
}
function ownKeys(e, r) {
  var t = Object.keys(e);
  if (Object.getOwnPropertySymbols) {
    var o = Object.getOwnPropertySymbols(e);
    r && (o = o.filter(function(r2) {
      return Object.getOwnPropertyDescriptor(e, r2).enumerable;
    })), t.push.apply(t, o);
  }
  return t;
}
function _objectSpread(e) {
  for (var r = 1; r < arguments.length; r++) {
    var t = null != arguments[r] ? arguments[r] : {};
    r % 2 ? ownKeys(Object(t), true).forEach(function(r2) {
      _defineProperty(e, r2, t[r2]);
    }) : Object.getOwnPropertyDescriptors ? Object.defineProperties(e, Object.getOwnPropertyDescriptors(t)) : ownKeys(Object(t)).forEach(function(r2) {
      Object.defineProperty(e, r2, Object.getOwnPropertyDescriptor(t, r2));
    });
  }
  return e;
}
function _defineProperty(e, r, t) {
  return (r = _toPropertyKey(r)) in e ? Object.defineProperty(e, r, { value: t, enumerable: true, configurable: true, writable: true }) : e[r] = t, e;
}
function _toPropertyKey(t) {
  var i = _toPrimitive(t, "string");
  return "symbol" == _typeof(i) ? i : i + "";
}
function _toPrimitive(t, r) {
  if ("object" != _typeof(t) || !t) return t;
  var e = t[Symbol.toPrimitive];
  if (void 0 !== e) {
    var i = e.call(t, r || "default");
    if ("object" != _typeof(i)) return i;
    throw new TypeError("@@toPrimitive must return a primitive value.");
  }
  return ("string" === r ? String : Number)(t);
}
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 GroupChatEdit = function GroupChatEdit2(_ref) {
  var _GroupChatMembersList;
  var allProfilesRef = _ref.allProfilesRef, _ref$Header = _ref.Header, Header = _ref$Header === void 0 ? _chunkA4GGIRWMjs.Header_default : _ref$Header, _ref$HeaderProps = _ref.HeaderProps, HeaderProps = _ref$HeaderProps === void 0 ? {} : _ref$HeaderProps, _ref$GroupChatMembers = _ref.GroupChatMembersList, GroupChatMembersList = _ref$GroupChatMembers === void 0 ? _chunkNSD4MMXMjs.GroupChatMembersList_default : _ref$GroupChatMembers, _ref$GroupChatMembers2 = _ref.GroupChatMembersListProps, GroupChatMembersListProps = _ref$GroupChatMembers2 === void 0 ? {} : _ref$GroupChatMembers2, onCancellation = _ref.onCancellation, onRemovalFromGroup = _ref.onRemovalFromGroup, onValidSubmission = _ref.onValidSubmission, profileId = _ref.profileId, queryRef = _ref.queryRef, roomId = _ref.roomId;
  var _useNotification = _utils.useNotification.call(void 0, ), sendToast = _useNotification.sendToast;
  var _useState = _react.useState.call(void 0, false), _useState2 = _slicedToArray(_useState, 2), open = _useState2[0], setOpen = _useState2[1];
  var smDown = _web.useResponsive.call(void 0, "down", "sm");
  var _usePreloadedQuery = _reactrelay.usePreloadedQuery.call(void 0, _chunkM4XNAJAZjs.GroupDetailsQuery, queryRef), group = _usePreloadedQuery.chatRoom;
  var _useGroupNameAndAvata = _chunkECBMCATXjs.useGroupNameAndAvatar.call(void 0, group), avatar = _useGroupNameAndAvata.avatar, title = _useGroupNameAndAvata.title;
  _chunkPQMT7VJNjs.useRoomListSubscription.call(void 0, {
    profileId,
    connections: [],
    onRemoval: onRemovalFromGroup
  });
  var _usePaginationFragmen = _reactrelay.usePaginationFragment.call(void 0, _chunk22ZA7RAXjs.MembersListFragment, group), membersList = _usePaginationFragmen.data, loadNext = _usePaginationFragmen.loadNext, isLoadingNext = _usePaginationFragmen.isLoadingNext, hasNext = _usePaginationFragmen.hasNext, refetch = _usePaginationFragmen.refetch;
  var participants = _react.useMemo.call(void 0, function() {
    var _membersList$particip;
    return membersList === null || membersList === void 0 || (_membersList$particip = membersList.participants) === null || _membersList$particip === void 0 || (_membersList$particip = _membersList$particip.edges) === null || _membersList$particip === void 0 ? void 0 : _membersList$particip.map(function(edge) {
      var _edge$node;
      return (edge === null || edge === void 0 || (_edge$node = edge.node) === null || _edge$node === void 0 ? void 0 : _edge$node.profile) && edge.node.profile;
    });
  }, [membersList]);
  var formReturn = _reacthookform.useForm.call(void 0, {
    defaultValues: _chunkWJSOX6WAjs.getDefaultFormValues.call(void 0, title || "", avatar),
    resolver: _zod.zodResolver.call(void 0, _chunkWJSOX6WAjs.DEFAULT_FORM_VALIDATION),
    mode: "onBlur"
  });
  var setValue = formReturn.setValue, watch = formReturn.watch, handleSubmit = formReturn.handleSubmit, _formReturn$formState = formReturn.formState, isValid = _formReturn$formState.isValid, isDirty = _formReturn$formState.isDirty, dirtyFields = _formReturn$formState.dirtyFields;
  var _useUpdateChatRoomMut = _chunkQRKOXN2Fjs.useUpdateChatRoomMutation.call(void 0, ), _useUpdateChatRoomMut2 = _slicedToArray(_useUpdateChatRoomMut, 2), commit = _useUpdateChatRoomMut2[0], isMutationInFlight = _useUpdateChatRoomMut2[1];
  var onSubmit = handleSubmit(function(data) {
    if (!roomId) return;
    var dirtyValues = _utils.filterDirtyValues.call(void 0, {
      values: data,
      dirtyFields
    });
    var image = data.image;
    var uploadables = {};
    if ("image" in dirtyValues) {
      if (image && (image instanceof File || image instanceof Blob)) {
        uploadables.image = image;
      } else {
        dirtyValues.deleteImage = true;
      }
      delete dirtyValues.image;
    }
    delete dirtyValues.participants;
    commit({
      variables: {
        input: _objectSpread({
          roomId,
          profileId
        }, dirtyValues),
        connections: []
      },
      uploadables,
      onCompleted: function onCompleted(response) {
        var _response$chatRoomUpd;
        var errors = response === null || response === void 0 || (_response$chatRoomUpd = response.chatRoomUpdate) === null || _response$chatRoomUpd === void 0 ? void 0 : _response$chatRoomUpd.errors;
        if (errors) {
          sendToast("Something went wrong", {
            type: "error"
          });
          _utils.setFormRelayErrors.call(void 0, formReturn, errors);
        } else {
          onValidSubmission();
        }
      }
    });
  });
  var isEditButtonDisabled = !isValid || !isDirty;
  var _useTransition = _react.useTransition.call(void 0, ), _useTransition2 = _slicedToArray(_useTransition, 2), isPending = _useTransition2[0], startTransition = _useTransition2[1];
  var handleAddMemberSuccess = function handleAddMemberSuccess2() {
    setOpen(false);
    startTransition(function() {
      refetch === null || refetch === void 0 || refetch({});
    });
  };
  if (smDown && open) return /* @__PURE__ */ _jsxruntime.jsx.call(void 0, _chunkKY3OAM4Ejs.AddMembersMobile_default, {
    allProfilesRef,
    onClose: function onClose() {
      return setOpen(false);
    },
    handleSubmitSuccess: handleAddMemberSuccess,
    profileId,
    roomId,
    isPending,
    existingMembers: participants
  });
  return /* @__PURE__ */ _jsxruntime.jsxs.call(void 0, _material.Box, {
    children: [/* @__PURE__ */ _jsxruntime.jsx.call(void 0, _chunkDHYUUR3Wjs.AddMembersDialog_default, {
      open,
      allProfilesRef,
      onClose: function onClose() {
        return setOpen(false);
      },
      handleSubmitSuccess: handleAddMemberSuccess,
      profileId,
      roomId,
      isPending,
      existingMembers: participants
    }), /* @__PURE__ */ _jsxruntime.jsx.call(void 0, Header, _objectSpread({
      isEditButtonDisabled,
      isMutationInFlight,
      onCancellation,
      onSubmit
    }, HeaderProps)), /* @__PURE__ */ _jsxruntime.jsx.call(void 0, _chunkWNUWPU4Yjs.EditGroupTitleAndImage_default, {
      form: formReturn,
      FORM_VALUE: _chunkNWQ2TTZQjs.CREATE_OR_EDIT_GROUP_FORM_VALUE,
      isMutationInFlight
    }), /* @__PURE__ */ _jsxruntime.jsx.call(void 0, GroupChatMembersList, _objectSpread({
      FORM_VALUE: _chunkNWQ2TTZQjs.CREATE_OR_EDIT_GROUP_FORM_VALUE,
      setValue,
      watch,
      currentParticipants: participants,
      refetch,
      membersLoadNext: loadNext,
      membersHasNext: hasNext,
      membersIsLoadingNext: isLoadingNext,
      MembersListProps: _objectSpread({
        allowAddMember: true,
        onAddMemberClick: function onAddMemberClick() {
          return setOpen(true);
        }
      }, (_GroupChatMembersList = GroupChatMembersListProps === null || GroupChatMembersListProps === void 0 ? void 0 : GroupChatMembersListProps.MembersListProps) !== null && _GroupChatMembersList !== void 0 ? _GroupChatMembersList : {})
    }, GroupChatMembersListProps))]
  });
};
var WrappedEditGroup = function WrappedEditGroup2(props) {
  var _useCurrentProfile = _authentication.useCurrentProfile.call(void 0, ), currentProfile = _useCurrentProfile.currentProfile;
  if (!(currentProfile !== null && currentProfile !== void 0 && currentProfile.id)) {
    return null;
  }
  return /* @__PURE__ */ _jsxruntime.jsx.call(void 0, GroupChatEdit, _objectSpread({
    profileId: currentProfile.id
  }, props));
};
var GroupChatEdit_default = WrappedEditGroup;



exports.GroupChatEdit_default = GroupChatEdit_default;
//# sourceMappingURL=chunk-7SPJVV6Y.js.map