import {Dialog} from './base_components/dialog/dialog';


export function setErrors(errors: any[], controls, dialog: Dialog) {
  let errorStrings: string[] = [];
  for (const key in errors) {
    const err = errors[key];
    let errorKey = '';
    try {
      errorKey = (err.source.pointer).split('\/').reverse()[0] + 'Ctrl';
    } catch (e) {
      errorKey = '';
    }
    if (errorKey !== '') {
      // ERRPR KEY HAMON INDEX HAST
      controls[errorKey].setErrors({'error': err.detail});
    } else {
      errorStrings[errorStrings.length] = err.detail;
    }
  }
  // if (errorStrings.length > 0) {
  //   // dialog.openForAlert(errorStrings.join(','));
  // }
}

// az server username barmigardone
export function repairErrors(errors) {
  for (const key in errors) {
    if (errors[key]['source'] && (errors[key]['source']['pointer'] == '/data/attribute/email' || errors[key]['source']['pointer'] == '/data/attribute/mobile')) {
      errors[key]['source']['pointer'] = '/data/attribute/username';
    }
  }
  return errors;
}

