/**
 * @license
 * Copyright Google LLC All Rights Reserved.
 *
 * Use of this source code is governed by an MIT-style license that can be
 * found in the LICENSE file at https://angular.dev/license
 */

import type { Compilation, LoaderContext } from 'webpack';

export const InlineAngularResourceLoaderPath: string = __filename;

export const InlineAngularResourceSymbol: unique symbol = Symbol(
  '@ngtools/webpack[angular-resource]',
);

export interface CompilationWithInlineAngularResource extends Compilation {
  [InlineAngularResourceSymbol]: string;
}

export default function (this: LoaderContext<{ data?: string }>): void {
  const callback = this.async();
  const { data } = this.getOptions();

  if (data) {
    callback(undefined, Buffer.from(data, 'base64').toString());
  } else {
    const content = (this._compilation as CompilationWithInlineAngularResource)[
      InlineAngularResourceSymbol
    ];
    callback(undefined, content);
  }
}
