/**
 * Copyright (c) Meta Platforms, Inc. and affiliates.
 *
 * This source code is licensed under the MIT license found in the
 * LICENSE file in the root directory of this source tree.
 *
 * @format
 */

import {OperatorConfig} from '../PowerSearch';

export type PowerSearchOperatorProcessor = (
  powerSearchOperatorConfig: OperatorConfig,
  searchValue: any,
  value: any,
) => boolean;

export const dataTablePowerSearchOperators = {
  string_contains: () => ({
    label: 'contains',
    key: 'string_contains',
    valueType: 'STRING',
  }),
  string_not_contains: () => ({
    label: 'does not contain',
    key: 'string_not_contains',
    valueType: 'STRING',
  }),
  string_matches_exactly: () => ({
    label: 'is',
    key: 'string_matches_exactly',
    valueType: 'STRING',
  }),
  string_not_matches_exactly: () => ({
    label: 'is not',
    key: 'string_not_matches_exactly',
    valueType: 'STRING',
  }),
  string_set_contains_any_of: () => ({
    label: 'contains any of',
    key: 'string_set_contains_any_of',
    valueType: 'STRING_SET',
  }),
} satisfies {
  [key: string]: (...args: any[]) => OperatorConfig;
};

export type PowerSearchOperatorProcessorConfig = {
  [K in keyof typeof dataTablePowerSearchOperators]: PowerSearchOperatorProcessor;
};

export const dataTablePowerSearchOperatorProcessorConfig = {
  string_contains: (operator, searchValue, value) =>
    (value as string)
      .toLowerCase()
      .includes((searchValue as string).toLowerCase()),
  string_not_contains: (operator, searchValue, value) =>
    !(value as string)
      .toLowerCase()
      .includes((searchValue as string).toLowerCase()),
  string_matches_exactly: (operator, searchValue, value) =>
    value === searchValue,
  string_not_matches_exactly: (operator, searchValue, value) =>
    value !== searchValue,
  // See PowerSearchStringSetTerm
  string_set_contains_any_of: (operator, searchValue: string[], value) =>
    searchValue.some((item) => (value as string).toLowerCase().includes(item)),
} satisfies PowerSearchOperatorProcessorConfig;
