import { join, Path, strings } from '@angular-devkit/core';
import {
  chain,
  mergeWith,
  Rule,
  SchematicContext,
  Tree,
} from '@angular-devkit/schematics';

import {
  generateFilesFromTemplates,
  addRootModuleImportAndMetadata,
} from '../../schematics-utils/utils';

export function main(options: any): Rule {
  const testModuleOptions = Object.assign({}, options);
  testModuleOptions.name = strings.dasherize(
    options.name.toLowerCase() + '-test',
  );
  testModuleOptions.path = join(
    options.sourceRoot as Path,
    testModuleOptions.name,
  );

  return (tree: Tree, context: SchematicContext) => {
    return chain([
      mergeWith(
        generateFilesFromTemplates(
          testModuleOptions,
          './files/test-client',
          testModuleOptions.path,
        ),
      ),
      addRootModuleImportAndMetadata(testModuleOptions),
    ]);
  };
}
