'use strict';

function findField(fields, fieldId) {
  return fields.find(field => field.id === fieldId) || findNestedField(fields, fieldId);
}
function findNestedField(fields, fieldId) {
  if (fieldId.includes('.')) {
    const fieldIdParts = fieldId.split('.');
    const nestedFieldId = fieldIdParts[0];
    const nestedField = fields.find(field => field.id === nestedFieldId);
    if (nestedField.type === 'reverse-reference') {
      // the field needs to be retrieved from the fields inside the reverse-reference
      return nestedField.fields.find(field => field.id === fieldIdParts[1]);
    }
    return nestedField;
  }
  return undefined;
}
function findRootField(fields, fieldId) {
  let field = fields.find(field => field.id === fieldId);
  if (field) {
    return field;
  }
  if (fieldId === null || fieldId === void 0 ? void 0 : fieldId.includes('.')) {
    const nestedFieldId = fieldId.split('.')[0];
    return fields.find(field => field.id === nestedFieldId);
  }
  return undefined;
}

exports.findField = findField;
exports.findRootField = findRootField;
