"use strict";

Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.default = void 0;
var _styles = require("../styles");
var _native = require("@react-navigation/native");
var z = _interopRequireWildcard(require("zod"));
var _zod2 = require("@hookform/resolvers/zod");
var _reactHookForm = require("react-hook-form");
var _useUserCollection = require("../../../../hooks/collections/user/useUserCollection");
var _useSearchUserByDisplayNameCollection = require("../../../../hooks/collections/search/useSearchUserByDisplayNameCollection");
var _tsSdkReactNative = require("@amityco/ts-sdk-react-native");
function _interopRequireWildcard(e, t) { if ("function" == typeof WeakMap) var r = new WeakMap(), n = new WeakMap(); return (_interopRequireWildcard = function (e, t) { if (!t && e && e.__esModule) return e; var o, i, f = { __proto__: null, default: e }; if (null === e || "object" != typeof e && "function" != typeof e) return f; if (o = t ? n : r) { if (o.has(e)) return o.get(e); o.set(e, f); } for (const t in e) "default" !== t && {}.hasOwnProperty.call(e, t) && ((i = (o = Object.defineProperty) && Object.getOwnPropertyDescriptor(e, t)) && (i.get || i.set) ? o(f, t, i) : f[t] = e[t]); return f; })(e, t); }
function useAddMember({
  communityId
} = {}) {
  var _route$params, _values$search;
  const {
    styles,
    theme
  } = (0, _styles.useStyles)();
  const route = (0, _native.useRoute)();
  const schema = z.object({
    users: z.array(z.custom()).min(1),
    search: z.string().optional()
  });
  const {
    watch,
    control,
    handleSubmit,
    formState: {
      isDirty,
      isSubmitting,
      isValid
    }
  } = (0, _reactHookForm.useForm)({
    resolver: (0, _zod2.zodResolver)(schema),
    mode: 'onChange',
    defaultValues: {
      search: '',
      users: ((_route$params = route.params) === null || _route$params === void 0 ? void 0 : _route$params.users) ?? []
    }
  });
  const values = watch();
  const userCollection = (0, _useUserCollection.useUserCollection)({
    enabled: !communityId,
    params: {
      limit: 20
    }
  });
  const searchUserDisplayNameCollection = (0, _useSearchUserByDisplayNameCollection.useSearchUserByDisplayNameCollection)({
    params: {
      displayName: values.search,
      limit: 20,
      searchBy: [_tsSdkReactNative.SearchUsersByEnum.DISPLAY_NAME],
      matchType: _tsSdkReactNative.UserRepository.AmityUserSearchMatchType.PARTIAL
    },
    enabled: !!values.search && !communityId
  });
  const onSubmit = () => {
    var _route$params2;
    (_route$params2 = route.params) !== null && _route$params2 !== void 0 && _route$params2.onAddedAction ? route.params.onAddedAction(watch('users')) : null;
  };
  const collection = ((_values$search = values.search) === null || _values$search === void 0 ? void 0 : _values$search.length) > 0 ? searchUserDisplayNameCollection : userCollection;
  return {
    styles,
    theme,
    isDirty,
    isSubmitting,
    isValid,
    handleSubmit,
    control,
    collection,
    onSubmit,
    values
  };
}
var _default = exports.default = useAddMember;
//# sourceMappingURL=useAddMember.js.map