/**
 * Copyright 2024 Phenix Real Time Solutions, Inc. Confidential and Proprietary. All Rights Reserved.
 */
import {IAdvancedSelectItem} from 'components/ui/advanced-select';
import {capabilities} from 'constants/capabilities';
import {isEqual} from './validators';

export const mergeArrays = <T>(firstArray: T[], secondArray: T[]): T[] => {
  // This method is generic and we accept any types of arrays for it

  const longer = firstArray.length >= secondArray.length ? firstArray : secondArray;
  const shorter = secondArray.length <= firstArray.length ? secondArray : firstArray;
  const difference = [];

  shorter.forEach(recordInLongerArray => {
    const isPresent = longer.some(recordInShorterArray => isEqual(recordInShorterArray, recordInLongerArray));

    if (!isPresent) {
      difference.push(recordInLongerArray);
    }
  });

  return [...difference, ...longer];
};

export const findDefaultCapabilities = (...requestedCapabilities: string[]): IAdvancedSelectItem[] => {
  const foundCapabilities = new Map();

  for (const capability of capabilities) {
    if (requestedCapabilities.includes(capability.value) && !foundCapabilities.has(capability.value)) {
      foundCapabilities.set(capability.value, capability);

      if (foundCapabilities.size === requestedCapabilities.length) {
        break;
      }
    }
  }

  return Array.from(foundCapabilities.values());
};
