const path = require('path');
const fse = require('fs-extra');
const assert = require('yeoman-assert');
const helpers = require('yeoman-test');
const expectedFiles = require('../utils/expected-files');
const LanguagesGenerator = require('../../generators/languages');

const mockBlueprintSubGen = class extends LanguagesGenerator {
  constructor(args, opts) {
    super(args, { fromBlueprint: true, ...opts }); // fromBlueprint variable is important

    const jhContext = (this.jhipsterContext = this.options.jhipsterContext);

    if (!jhContext) {
      this.error('This is a JHipster blueprint and should be used only like jhipster --blueprints myblueprint');
    }

    this.configOptions = jhContext.configOptions || {};
  }

  get initializing() {
    return super._initializing();
  }

  get prompting() {
    return super._prompting();
  }

  get configuring() {
    return super._configuring();
  }

  get default() {
    return super._default();
  }

  get writing() {
    return super._writing();
  }
};

describe('JHipster languages generator with blueprint', () => {
  const blueprintNames = ['generator-jhipster-myblueprint', 'myblueprint'];

  blueprintNames.forEach(blueprintName => {
    describe(`generate language with blueprint option '${blueprintName}'`, () => {
      before(done => {
        helpers
          .run(path.join(__dirname, '../../generators/languages'))
          .inTmpDir(dir => {
            fse.copySync(path.join(__dirname, '../../test/templates/ngx-blueprint'), dir);
          })
          .withOptions({
            fromCli: true,
            skipInstall: true,
            blueprint: blueprintName,
            skipChecks: true,
          })
          .withGenerators([[mockBlueprintSubGen, 'jhipster-myblueprint:languages']])
          .withPrompts({
            languages: ['de'],
          })
          .on('end', done);
      });

      it('creates expected files from jhipster languages generator', () => {
        assert.file(expectedFiles.i18nDeJson);
        assert.file(expectedFiles.i18nAdminDeJson);
      });
    });
  });
});
