import { PCN_POST_VIDEO } from '@/modules/@common/acl/constants';
import { ACCESS_LEVEL_ENUM, ACCESS_USER_ROLES } from '@/modules/editorial/shareAndAccess/constants';

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

const canShowTrim = (video, userId, userPermissions) => {
  const {
    access: { users, level },
  } = video;
  const videoOwner = users?.find((user) => user.role === ACCESS_USER_ROLES.owner);
  const isPublisherAdminEditorUser = hasPermission(PCN_POST_VIDEO, userPermissions);
  const isVideoOwner = +(videoOwner?.id ?? 0) === userId;

  const canEditPrivateVideo = level === ACCESS_LEVEL_ENUM.private && isVideoOwner;
  const canEditPublicVideo = level === ACCESS_LEVEL_ENUM.public && isPublisherAdminEditorUser;
  const canEditSiteVideo = level === ACCESS_LEVEL_ENUM.site && isPublisherAdminEditorUser;

  return canEditPrivateVideo || canEditPublicVideo || canEditSiteVideo;
};

export default canShowTrim;
