import { PLAYER_TYPES, PLAYER_WIDTH_UNITS_PERCENT } from '@/modules/players/Editor/constants';

export const getPlayerWidth = (playerWidth, playerWidthUnit) => {
  const val = !Number.isNaN(parseInt(playerWidth, 10)) ? playerWidth : undefined;

  return val !== undefined && playerWidthUnit === PLAYER_WIDTH_UNITS_PERCENT
    ? `${playerWidth}${PLAYER_WIDTH_UNITS_PERCENT}`
    : val;
};

export const getEditorialPlaylistClipInformation = (playlist) => {
  if (!playlist) {
    return [];
  }
  return playlist.clips;
};

export const getPlayerWidgetName = (publisher, playerType, playerName) => {
  if (playerName) {
    return playerName;
  }
  if (!publisher || !playerType) {
    return undefined;
  }

  const playerTemplate = Object.values(PLAYER_TYPES).find((type) => type.id === playerType.id);
  if (!playerTemplate) {
    return undefined;
  }
  return publisher[playerTemplate.templateName]?.slug;
};

export const getPublisherName = (id, publisher, playerType) => {
  if (!publisher) {
    return undefined;
  }

  if (id) {
    return publisher.slug;
  }
  const playerTemplate = Object.values(PLAYER_TYPES).find((type) => type.id === playerType.id);
  const publisherTemplate = publisher[playerTemplate.templateName];
  return publisherTemplate?.publisher?.slug;
};
