/**
 * Adapted from a private function at ng-packagr
 * https://github.com/ng-packagr/ng-packagr/blob/main/src/lib/ts/tsconfig.ts#L12:
 *
 * Changes made:
 * - Added an extra function that updates the configFilePath in the returned parsed options
 * to be the original tsconfig file.
 */

import type {
  CompilerOptions,
  ParsedConfiguration,
} from '@angular/compiler-cli';
import { ngCompilerCli } from 'ng-packagr/lib/utils/ng-compiler-cli';
import { resolve } from 'path';
import * as ts from 'typescript';

async function readDefaultTsConfig(
  fileName: string
): Promise<ParsedConfiguration> {
  // these options are mandatory
  const extraOptions: CompilerOptions = {
    target: ts.ScriptTarget.ES2020,
    experimentalDecorators: true,

    // sourcemaps
    sourceMap: false,
    inlineSources: true,
    inlineSourceMap: true,

    outDir: '',
    declaration: true,

    // ng compiler to options
    enableResourceInlining: true,

    // these are required to set the appropriate EmitFlags
    flatModuleId: 'AUTOGENERATED',
    flatModuleOutFile: 'AUTOGENERATED',
  };

  const { readConfiguration } = await ngCompilerCli();

  return readConfiguration(fileName, extraOptions);
}

/**
 * Proxy function that ensures the configFilePath option points to the original file path.
 */
export async function parseRemappedTsConfigAndMergeDefaults(
  workspaceRoot: string,
  originalFilePath: string,
  remappedFilePath: string
): Promise<ParsedConfiguration> {
  const parsedConfiguration = await readDefaultTsConfig(remappedFilePath);
  parsedConfiguration.options.configFilePath = resolve(
    workspaceRoot,
    originalFilePath
  );

  return parsedConfiguration;
}
