import { QA_CUSTOM_DEV_INSTANCE_ID, isStaging } from 'src/constants/env';
import { Step } from 'src/types/forms';

import {
  EDEMSA_INSTANCE_ID,
  EDEMSA_INSTANCE_ID_2,
  SPECIAL_FORMS,
} from './constants';
import { EdemsaSpecialFields } from './types';

const isEdemsaInstance = (instanceId: number) =>
  instanceId === EDEMSA_INSTANCE_ID ||
  instanceId === EDEMSA_INSTANCE_ID_2 ||
  (isStaging && instanceId === QA_CUSTOM_DEV_INSTANCE_ID);

export const isEdemsaSpecialForm = (instanceId: number, formName: string) =>
  isEdemsaInstance(instanceId) && SPECIAL_FORMS.includes(formName);

export const isEdemsaSpecialField = (
  description: string,
  instanceId: number,
  step: number,
) =>
  isEdemsaInstance(instanceId) &&
  Object.values(EdemsaSpecialFields).includes(
    description as EdemsaSpecialFields,
  ) &&
  step === 0;

export const iterateQuestions = (s: Step, callback: Function) => {
  return s?.questions?.reduce((acc, config, index) => {
    return callback(config, index, acc);
  }, 0);
};
