import elementsTitles, {
  tablesTitles
} from 'client/Containers/Calculation/Elements/titles';
import { TableNames } from 'client/Containers/Calculation/types/tables';
import { ICalculationStore } from 'core/types/Calculation/Store';
import { omit } from 'lodash';
import { toJS } from 'mobx';

export default function (this: ICalculationStore) {
  const invalidElements: string[] = [];
  const { validations: storeValidations } = this;
  const validations = toJS(storeValidations);
  Object.keys(validations).forEach(elementName => {
    if (validations[elementName] === false) {
      invalidElements.push(elementsTitles[elementName]);
    }
  });

  const invalidTables: string[] = [];
  const { tables: storeTables } = this;
  const tables = omit(toJS(storeTables), tablesExclusion);
  Object.keys(tables).forEach(tableName => {
    if (
      !tables[tableName].rows ||
      tables[tableName].rows.length === 0 ||
      tables[tableName].rows.some(row =>
        Object.keys(row).some(propName => row[propName].validation === false),
      )
    ) {
      invalidTables.push(tablesTitles[tableName]);
    }
  });

  return { invalidElements, invalidTables };
}

const tablesExclusion: TableNames[] = ['tableResults'];
