import { ACCESS_USER_ROLES, PRIVATE, PUBLIC, SITE } from '../constants';
import { PCN_PUT_VIDEO } from '@/modules/@common/acl/constants';

import { hasPermission } from '@/modules/@common/user/helpers';

export const canShare = (video, userPublisherIds, userId) => {
  if (video?.access?.level === PRIVATE) {
    const owner = video.access.users?.find((user) => user.role === ACCESS_USER_ROLES.owner);

    return owner?.id === `${userId}`;
  }

  if (video?.access?.level === SITE) {
    const publisherIds = userPublisherIds;
    return publisherIds?.some((id) => video.access.sites?.find((site) => id === parseInt(site.id, 10)));
  }

  return true;
};

export const canChangeAccess = (video, userId, userPermissions) => {
  if (video?.access?.level === PRIVATE) {
    const owner = video.access.users?.find((user) => user.role === ACCESS_USER_ROLES.owner);

    return owner?.id === `${userId}`;
  }

  if (video?.access?.level === SITE || video?.access?.level === PUBLIC) {
    const owner = video.access.users?.find((user) => user.role === ACCESS_USER_ROLES.owner);

    if (owner) {
      return hasPermission(PCN_PUT_VIDEO, userPermissions) || owner?.id === `${userId}`;
    }
  }

  return hasPermission(PCN_PUT_VIDEO, userPermissions);
};
