import { Chance } from 'chance';
import {
  InMemoryBackend,
  InMemoryBackendParams,
} from '@wix/bex-core/in-memory-backend';
import { Status } from './status';
import escapeRegExp from 'lodash/escapeRegExp';

export const createStatusesBackend = (
  params?: Partial<InMemoryBackendParams<Status, {}>>,
) => {
  const chance = new Chance();

  return new InMemoryBackend<Status>({
    createOne: () => ({
      id: chance.guid(),
      name: chance.name(),
    }),
    itemKey: (item) => item.id,
    total: 1000,
    predicate: ({ search }) => {
      const rgx = new RegExp(escapeRegExp(search), 'i');
      return (item) => {
        if (search && !rgx.test(item.name)) {
          return false;
        }

        return true;
      };
    },
    ...params,
  });
};
