"use strict";

Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.default = void 0;
var _react = _interopRequireWildcard(require("react"));
var _ProfileImagePlaceholder = require("../../MainComponents/ProfileImagePlaceholder");
var _Button = _interopRequireDefault(require("../../styled/Button"));
var _StyledInput = _interopRequireDefault(require("../../styled/StyledInput"));
var _ModalHeaderComponent = _interopRequireDefault(require("../ModalHeaderComponent"));
var _styledModalComponents = require("../styledModalComponents");
var _user = require("../../../networking/api-requests/user.api");
var _reactRedux = require("react-redux");
var _chatSettingsSlice = require("../../../roomStore/chatSettingsSlice");
var _reactNative = require("react-native");
var _jsxRuntime = require("react/jsx-runtime");
function _interopRequireDefault(e) { return e && e.__esModule ? e : { default: e }; }
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); }
// import { actionUpdateUser } from '../actions';

const base64ToFile = (base64String, fileName) => {
  const byteString = atob(base64String.split(',')[1]);
  const arrayBuffer = new ArrayBuffer(byteString.length);
  const uintArray = new Uint8Array(arrayBuffer);
  for (let i = 0; i < byteString.length; i++) {
    uintArray[i] = byteString.charCodeAt(i);
  }
  const blob = new Blob([uintArray], {
    type: 'image/jpeg'
  });
  return new File([blob], fileName, {
    type: 'image/jpeg',
    lastModified: Date.now()
  });
};
const EditUserModal = ({
  setIsEditing,
  modalUser,
  config
}) => {
  const dispatch = (0, _reactRedux.useDispatch)();
  const [firstName, setFirstName] = (0, _react.useState)(modalUser?.firstName || '');
  const [lastName, setLastName] = (0, _react.useState)(modalUser?.lastName || '');
  const [description, setDescription] = (0, _react.useState)(modalUser?.description || '');
  const [profileImage, setProfileImage] = (0, _react.useState)(modalUser?.profileImage);
  const onSave = async () => {
    try {
      let fd = new FormData();
      if (typeof profileImage === 'string' && profileImage.startsWith('data:image/')) {
        const file = base64ToFile(profileImage, 'profileImage.jpg');
        fd.append('file', file);
      } else if (profileImage instanceof File) {
        fd.append('file', profileImage);
      }
      fd.append('firstName', firstName);
      fd.append('lastName', lastName);
      fd.append('description', description);
      const {
        user
      } = await (0, _user.updateProfile)(fd);
      dispatch((0, _chatSettingsSlice.updateUser)({
        updates: {
          firstName,
          lastName,
          description,
          profileImage: user?.profileImage
        }
      }));
      setIsEditing(false);
    } catch (error) {
      console.log('error', error);
    }
  };
  const handleProfileImageChange = image => {
    setProfileImage(image);
  };
  return /*#__PURE__*/(0, _jsxRuntime.jsxs)(_jsxRuntime.Fragment, {
    children: [/*#__PURE__*/(0, _jsxRuntime.jsx)(_ModalHeaderComponent.default
    // leftMenu={
    //   <Button
    //     onPress={() => setIsEditing(false)}
    //     style={{
    //       paddingVertical: 13,
    //       paddingHorizontal: 8,
    //       width: "100%",
    //     }}
    //     color="#00"
    //   >
    //     Cancel
    //   </Button>
    // }
    , {
      handleCloseModal: () => setIsEditing(false),
      rightMenu: /*#__PURE__*/(0, _jsxRuntime.jsx)(_Button.default, {
        onPress: onSave,
        variant: "outlined",
        style: {
          width: 128
        },
        color: "#0052CD",
        text: "Save"
      })
    }), /*#__PURE__*/(0, _jsxRuntime.jsx)(_styledModalComponents.CenterContainer, {
      children: /*#__PURE__*/(0, _jsxRuntime.jsx)(_ProfileImagePlaceholder.ProfileImagePlaceholder, {
        icon: profileImage,
        name: `${firstName} ${lastName}`,
        size: 120,
        upload: {
          onUpload: handleProfileImageChange,
          active: true
        }
      })
    }), /*#__PURE__*/(0, _jsxRuntime.jsxs)(_reactNative.View, {
      style: {
        display: 'flex',
        flexDirection: 'column',
        gap: 32,
        width: '100%',
        justifyContent: 'center',
        alignItems: 'center'
      },
      children: [/*#__PURE__*/(0, _jsxRuntime.jsx)(_StyledInput.default, {
        color: config?.colors?.primary,
        placeholder: "First Name",
        label: "First Name",
        value: firstName,
        onChangeText: text => setFirstName(text)
      }), /*#__PURE__*/(0, _jsxRuntime.jsx)(_StyledInput.default, {
        color: config?.colors?.primary,
        placeholder: "Last Name",
        label: "Last Name",
        value: lastName,
        onChangeText: text => setLastName(text)
      }), /*#__PURE__*/(0, _jsxRuntime.jsx)(_StyledInput.default, {
        color: config?.colors?.primary,
        placeholder: "About",
        label: "About",
        value: description,
        onChangeText: text => setDescription(text)
      })]
    })]
  });
};
var _default = exports.default = EditUserModal;
//# sourceMappingURL=EditUserModal.js.map