import { ADDITIONAL_FILTER_NAMES, FILTERS_SHOULD_HIDE_IN_AI_BLOCK_FILTER } from '../constants';
import { VIDEO_ADMIN, VIDEO_FILTERS_ALL, VIDEO_FILTERS_INTERNAL_BUSINESS } from '@/modules/@common/acl/constants';
import { ACCOUNT, INTERNAL } from '@/modules/@common/user/constants/rolesType';

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

const FILTER_HUBS = 'hubs';
const FILTER_SOURCE_TYPE = 'sourceType';
const FILTER_MEDIA_TYPE = 'mediaType';

export const getFiltersNamesByPermission = (aiPlaylistMode, userRoleType, userPermissions) => {
  const filterKey =
    [VIDEO_FILTERS_ALL, VIDEO_FILTERS_INTERNAL_BUSINESS, VIDEO_ADMIN].find((perm) =>
      hasPermission(perm, userPermissions),
    ) ?? VIDEO_FILTERS_ALL;

  const filterKeys = Object.keys(ADDITIONAL_FILTER_NAMES[filterKey]);
  const filterNames = filterKeys.filter((filter) => {
    // Condition 1: Exclude filters if in AI playlist mode and they should be hidden
    const shouldHideInAiBlock = aiPlaylistMode && FILTERS_SHOULD_HIDE_IN_AI_BLOCK_FILTER.includes(filter);
    if (shouldHideInAiBlock) {
      return false;
    }

    // Condition 2: Include FILTER_HUBS, FILTER_MEDIA_TYPE only if the user role type is ACCOUNT
    if ([FILTER_HUBS, FILTER_MEDIA_TYPE].includes(filter)) {
      return userRoleType === ACCOUNT;
    }

    // Condition 3: Include SOURCE_TYPE only if the user role type is INTERNAL
    if (filter === FILTER_SOURCE_TYPE) {
      return userRoleType === INTERNAL;
    }

    // Include all other filters
    return true;
  });

  return filterNames;
};

export default {
  getFiltersNamesByPermission,
};
