import { HttpStatus, UnprocessableEntityException } from '@nestjs/common';

export async function validateUsedInFormula(formulaDataService, name) {
  const formulas = await formulaDataService.getManyByOptions();
  formulas?.forEach((formula) => {
    const allWords = formula?.formula_string
      ?.replace(/[^a-zA-Z0-9]/g, ' ')
      .split(' ');
    if (allWords.find((item) => item.toLowerCase() == name))
      throw new UnprocessableEntityException({
        statusCode: HttpStatus.UNPROCESSABLE_ENTITY,
        message: `Gagal! pajak dengan nama ${name} telah dogunakan pada formula ${formula.type}`,
        error: 'Unprocessable Entity',
      });
  });
}
