function getExtensionModuleField(manifest, fieldType, handlerLink) {
  if (!handlerLink.type) {
    throw new Error(`Missing type!`);
  }
  if (!manifest.modules.fields) {
    throw new Error(`No definition of fields for extension type "${manifest.type}" and key "${manifest.key}"`);
  }
  if (!manifest.modules.fields[fieldType]) {
    throw new Error(`No definition for field type "${fieldType}" on manifest for extension with type "${manifest.type}" and key "${manifest.key}"`);
  }
  const {
    type
  } = handlerLink;
  // Ignored via go/ees005
  // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
  const handler = manifest.modules.fields[fieldType][type];
  if (!handler) {
    throw new Error(`No handler of type "${type}" for extension type "${manifest.type}" and key "${manifest.key}"`);
  }
  return handler;
}

/** attempt to get the custom resolver for this field, or throw */
export function getCustomFieldResolver(manifest, handlerLink) {
  const handler = getExtensionModuleField(manifest, 'custom', handlerLink);
  return handler.resolver;
}

/** attempt to get the serializer for this field, or throw */
export function getFieldSerializer(manifest, handlerLink) {
  const handler = getExtensionModuleField(manifest, 'fieldset', handlerLink);
  return handler.serializer;
}

/** attempt to get the deserializer for this field, or throw */
export function getFieldDeserializer(manifest, handlerLink) {
  const handler = getExtensionModuleField(manifest, 'fieldset', handlerLink);
  return handler.deserializer;
}

/** attempt to get the user field context provider for this field, or throw */
export function getUserFieldContextProvider(manifest, handlerLink) {
  const handler = getExtensionModuleField(manifest, 'user', handlerLink);
  return handler.provider;
}