import {
  CONVERSATION_ITEM_HEIGHT,
  LOCAL_INITIAL_VERSION,
} from '@modules/chats/constants';
import {
  ChatUser,
  ChatUserStatus,
  Asset,
  HuUser,
  MediaType,
  AssetFormat,
} from '@modules/chats/interfaces';
import {API_URL} from '@shared/keys';

import {isDocumentAsset, isPictureAsset} from './mappers';

export const getChatItemLayout = (_data: unknown, index: number) => {
  return {
    length: CONVERSATION_ITEM_HEIGHT,
    offset: CONVERSATION_ITEM_HEIGHT * index,
    index,
  };
};

export const convertHuUserToChatUser = (user: HuUser) => {
  const chatUser: ChatUser = {
    id: user.id.toString(),
    hu_data: {
      member: {
        id: user.id,
        email: user.email || '',
        first_name: user.firstName,
        last_name: user.lastName,
        employee_internal_id: user.employeeInternalId || '',
        picture_asset_url: user.profilePicture || '',
        status: user.active
          ? ChatUserStatus.Active
          : user.deleted
          ? ChatUserStatus.Deleted
          : ChatUserStatus.Deactivated,
        version: LOCAL_INITIAL_VERSION,
      },
    },
    name: `${user.firstName} ${user.lastName}`,
    tz: '',
    profile: {
      email: user.email || '',
      image_24: null,
      image_32: null,
      image_48: null,
      image_72: null,
      image_512: null,
      image_192: null,
    },
    updated: null,
  };
  return chatUser;
};

export const prepareAttachmentForMessage = (
  attachment: Asset,
  channel: string,
): Asset => {
  if (isPictureAsset(attachment)) {
    return {
      ...attachment,
      image_url: generateAssetUrlForMessage(attachment?.id || 0, channel),
    };
  }
  if (isDocumentAsset(attachment)) {
    return {
      ...attachment,
      url: generateAssetUrlForMessage(attachment?.id || 0, channel),
    };
  }
  return attachment;
};

export const generateAssetUrlForMessage = (
  assetId: number,
  channelId: string,
) => {
  return `${API_URL}/marty/assets/${assetId}/conversation/${channelId}`;
};

export const getMediaTypeByFormat = (format?: AssetFormat) => {
  switch (format) {
    case 'image':
      return MediaType.IMAGE;
    case 'video':
      return MediaType.VIDEO;
    case 'audio':
      return MediaType.AUDIO;
    case 'file':
      return MediaType.FILE;
    case 'gif':
      return MediaType.GIF;
    default:
      return MediaType.FILE;
  }
};

export * from './socketEvents';
export * from './errors';
export * from './measuring';
export * from './mappers';
export * from './mentions';
export * from './blocks';
export * from './amplitude';
export * from './reactions';
export * from './debounce';
export * from './compression';
export * from './files';
export * from './avatar';
export * from './messages';
export * from './date';
export * from './getChatImage';
export * from './getPreviewAsset';
export * from './imageSizes';
export * from './prefetchMessageImages';
export * from './chatSnackbar';
