import ApiResource from '~/models/ApiResource'

/**
 * Decorates an ApiResource's property to signal it as a field that is provided
 * as an IRI or an array of IRI by the API
 *
 * If the property is decorated, the HydraNormalizer will parse the IRI when de-normalizing
 * to get the id(s), then re-encode it as IRI(s) when re-normalizing.
 */
export function IriEncoded(apiResource: typeof ApiResource): PropertyDecorator {
  // We have to comply with the PropertyDecorator return type
  // eslint-disable-next-line @typescript-eslint/ban-types
  return (target: Object, propertyKey: string | symbol) => {
    // @ts-expect-error The object is an ApiResource
    const self = target.$self()

    self.addIriEncodedField(propertyKey as string, apiResource)
  }
}
