import {GifAttachment} from '@interfaces/attachments';
import {Poll} from '@interfaces/poll';
import {LinkPreview} from '@interfaces/linkPreview';
import {getGifProps} from '@shared/utils';
import {windowDimensions} from '@shared/constants';

import {GIF_WIDTH} from './constants';

const DEFAULT_ASPECT_RATIO = 1;
const POST_WIDTH = windowDimensions.width;
const POST_HEIGHT = windowDimensions.height * 0.3;

export const getPostBodyGifProps = (
  attachment: GifAttachment,
  isComment?: boolean,
) => {
  const aspectRatio =
    attachment?.width && attachment?.height
      ? attachment.width / attachment.height
      : DEFAULT_ASPECT_RATIO;
  return getGifProps({
    aspectRatio,
    maxHeight: POST_HEIGHT,
    maxWidth: isComment ? GIF_WIDTH : POST_WIDTH,
  });
};

export const checkHasPoll = (poll: Nullable<Poll>): poll is Poll =>
  !!poll?.pollOptions?.length;

export const getLinkPreview = (
  linkPreviews: Nullable<Record<string, Nullable<LinkPreview>>>,
) => {
  if (
    !linkPreviews ||
    !Object.values(linkPreviews)?.length ||
    !Object.values(linkPreviews)?.at(0)
  ) {
    return undefined;
  }
  return {
    ...Object.values(linkPreviews)[0],
    url: Object.keys(linkPreviews)[0],
  } as LinkPreview & {url: string};
};
