import { type DefaultItemType } from '@composed-components/ConditionLine/types';

import { type CriteriaEntry, CriteriaType } from './types';

/**
 * Returns the criteria types that are not yet selected, filtered to `allowed`,
 * excluding ALL (which is never offered via CriteriaAutocomplete).
 * Defaults to [SEGMENTATION, INDIVIDUAL] when `allowed` is omitted.
 */
export const getAvailableCriteriaTypes = <
  FieldItemType extends DefaultItemType,
  ValueItemType extends DefaultItemType,
>(
  criterias: CriteriaEntry<FieldItemType, ValueItemType>[],
  allowed: CriteriaType[] = [
    CriteriaType.SEGMENTATION,
    CriteriaType.INDIVIDUAL,
  ],
): CriteriaType[] => {
  const selectedTypes = new Set(criterias.map(c => c.type));

  if (selectedTypes.has(CriteriaType.ALL)) return [];

  return allowed
    .filter(t => t !== CriteriaType.ALL)
    .filter(type => !selectedTypes.has(type));
};
