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

var _chunkZLMZCINBjs = require('./chunk-ZLMZCINB.js');


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


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




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


var _chunk6KMONHAUjs = require('./chunk-6KMONHAU.js');


var _chunkV2TU2PN3js = require('./chunk-V2TU2PN3.js');


var _chunkDTIR4DVOjs = require('./chunk-DTIR4DVO.js');


var _chunkBV6NSNQ2js = require('./chunk-BV6NSNQ2.js');

// tmp-babel/messages/web/GroupChatCreate/index.js
var _react = require('react');
var _authentication = require('@baseapp-frontend/authentication');
var _utils = require('@baseapp-frontend/utils');
var _zod = require('@hookform/resolvers/zod');
var _material = require('@mui/material');
var _reacthookform = require('react-hook-form');
var _relayruntime = require('relay-runtime');
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 GroupChatCreate = function GroupChatCreate2(_ref) {
  var allProfilesRef = _ref.allProfilesRef, _ref$EditGroupTitleAn = _ref.EditGroupTitleAndImage, EditGroupTitleAndImage = _ref$EditGroupTitleAn === void 0 ? _chunkWNUWPU4Yjs.EditGroupTitleAndImage_default : _ref$EditGroupTitleAn, _ref$EditGroupTitleAn2 = _ref.EditGroupTitleAndImageProps, EditGroupTitleAndImageProps = _ref$EditGroupTitleAn2 === void 0 ? {} : _ref$EditGroupTitleAn2, _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, _ref$Header = _ref.Header, Header = _ref$Header === void 0 ? _chunkZLMZCINBjs.Header_default : _ref$Header, _ref$HeaderProps = _ref.HeaderProps, HeaderProps = _ref$HeaderProps === void 0 ? {} : _ref$HeaderProps, onValidSubmission = _ref.onValidSubmission, onBackButtonClicked = _ref.onBackButtonClicked;
  var _useNotification = _utils.useNotification.call(void 0, ), sendToast = _useNotification.sendToast;
  var _useAllProfilesList = _chunkBV6NSNQ2js.useAllProfilesList.call(void 0, allProfilesRef), allProfiles = _useAllProfilesList.data.allProfiles, loadNext = _useAllProfilesList.loadNext, isLoadingNext = _useAllProfilesList.isLoadingNext, hasNext = _useAllProfilesList.hasNext, refetch = _useAllProfilesList.refetch;
  var formReturn = _reacthookform.useForm.call(void 0, {
    defaultValues: _chunkNWQ2TTZQjs.DEFAULT_CREATE_OR_EDIT_GROUP_FORM_VALUE,
    // @ts-ignore TODO: check typing issue with zodResolver
    resolver: _zod.zodResolver.call(void 0, _chunkNWQ2TTZQjs.DEFAULT_CREATE_OR_EDIT_GROUP_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 _useCurrentProfile = _authentication.useCurrentProfile.call(void 0, ), currentProfile = _useCurrentProfile.currentProfile;
  var _useCreateChatRoomMut = _chunkV2TU2PN3js.useCreateChatRoomMutation.call(void 0, ), _useCreateChatRoomMut2 = _slicedToArray(_useCreateChatRoomMut, 2), commit = _useCreateChatRoomMut2[0], isMutationInFlight = _useCreateChatRoomMut2[1];
  var _useChatRoom = _chunkDTIR4DVOjs.useChatRoom_default.call(void 0, ), setChatRoom = _useChatRoom.setChatRoom;
  var onSubmit = handleSubmit(function(data) {
    var dirtyValues = _utils.filterDirtyValues.call(void 0, {
      values: data,
      dirtyFields
    });
    var title = data.title, participants2 = data.participants, image = data.image;
    var participantsIds = (participants2 || []).map(function(member) {
      return member === null || member === void 0 ? void 0 : member.id;
    });
    var uploadables = {};
    if (_chunkNWQ2TTZQjs.CREATE_OR_EDIT_GROUP_FORM_VALUE.image in dirtyValues && image && typeof image !== "string") {
      uploadables.image = image;
    }
    commit({
      variables: {
        input: {
          title,
          isGroup: true,
          profileId: currentProfile === null || currentProfile === void 0 ? void 0 : currentProfile.id,
          participants: participantsIds
        },
        connections: [
          // TODO: add filter handling (for now we can default 'unreadMessages' to false)
          _relayruntime.ConnectionHandler.getConnectionID(currentProfile === null || currentProfile === void 0 ? void 0 : currentProfile.id, "roomsList_chatRooms", {
            unreadMessages: false,
            archived: false,
            q: ""
          })
        ]
      },
      uploadables,
      onCompleted: function onCompleted(response) {
        var _response$chatRoomCre;
        var errors = response === null || response === void 0 || (_response$chatRoomCre = response.chatRoomCreate) === null || _response$chatRoomCre === void 0 ? void 0 : _response$chatRoomCre.errors;
        if (errors) {
          sendToast("Something went wrong", {
            type: "error"
          });
          _utils.setFormRelayErrors.call(void 0, formReturn, errors);
        } else {
          var _response$chatRoomCre2;
          setChatRoom({
            id: response === null || response === void 0 || (_response$chatRoomCre2 = response.chatRoomCreate) === null || _response$chatRoomCre2 === void 0 || (_response$chatRoomCre2 = _response$chatRoomCre2.room) === null || _response$chatRoomCre2 === void 0 || (_response$chatRoomCre2 = _response$chatRoomCre2.node) === null || _response$chatRoomCre2 === void 0 ? void 0 : _response$chatRoomCre2.id
          });
          onValidSubmission();
        }
      }
    });
  });
  var participants = watch(_chunkNWQ2TTZQjs.CREATE_OR_EDIT_GROUP_FORM_VALUE.participants);
  var profiles = _react.useMemo.call(void 0, function() {
    return (allProfiles === null || allProfiles === void 0 ? void 0 : allProfiles.edges.filter(function(edge) {
      return (edge === null || edge === void 0 ? void 0 : edge.node) && (edge === null || edge === void 0 ? void 0 : edge.node.id) !== (currentProfile === null || currentProfile === void 0 ? void 0 : currentProfile.id) && !participants.some(function(member) {
        var _edge$node;
        return (member === null || member === void 0 ? void 0 : member.id) === (edge === null || edge === void 0 || (_edge$node = edge.node) === null || _edge$node === void 0 ? void 0 : _edge$node.id);
      });
    }).map(function(edge) {
      return edge === null || edge === void 0 ? void 0 : edge.node;
    })) || [];
  }, [allProfiles, participants, currentProfile === null || currentProfile === void 0 ? void 0 : currentProfile.id]);
  var isCreateButtonDisabled = !isValid || !isDirty || isMutationInFlight;
  return /* @__PURE__ */ _jsxruntime.jsxs.call(void 0, _material.Box, {
    children: [/* @__PURE__ */ _jsxruntime.jsx.call(void 0, Header, _objectSpread({
      isDisabled: isCreateButtonDisabled,
      onCreateButtonClicked: onSubmit,
      onBackButtonClicked
    }, HeaderProps)), /* @__PURE__ */ _jsxruntime.jsx.call(void 0, 
      EditGroupTitleAndImage,
      _objectSpread({
        form: formReturn,
        FORM_VALUE: _chunkNWQ2TTZQjs.CREATE_OR_EDIT_GROUP_FORM_VALUE,
        isMutationInFlight
      }, EditGroupTitleAndImageProps)
    ), /* @__PURE__ */ _jsxruntime.jsx.call(void 0, GroupChatMembersList, _objectSpread({
      FORM_VALUE: _chunkNWQ2TTZQjs.CREATE_OR_EDIT_GROUP_FORM_VALUE,
      setValue,
      watch,
      currentParticipants: participants,
      connections: profiles,
      refetch,
      connectionsLoadNext: loadNext,
      connectionsHasNext: hasNext,
      connectionsIsLoadingNext: isLoadingNext,
      ProfilesContainer: _chunk6KMONHAUjs.ProfilesContainer
    }, GroupChatMembersListProps))]
  });
};
var GroupChatCreate_default = GroupChatCreate;



exports.GroupChatCreate_default = GroupChatCreate_default;
//# sourceMappingURL=chunk-PSM4BT5U.js.map