/* eslint-disable flowtype/require-valid-file-annotation */
const path = require('path');
const inquirerDirectory = require('inquirer-directory');

module.exports = function(plop) {
  // Add ability to enter paths.
  plop.addPrompt('directory', inquirerDirectory);

  plop.setHelper('upperCamelCase', item => {
    const firstLetter = `${item[0].toUpperCase()}`;
    return `${firstLetter}${item.slice(1)}`;
  });

  plop.setHelper('componentBucket', item => {
    const [, bucket] = /(primitives|legacy|one-off|core)/.exec(item);
    return bucket.charAt(0).toUpperCase() + bucket.slice(1);
  });

  plop.setGenerator('test', {
    prompts: [
      {
        type: 'input',
        name: 'componentName',
        message:
          'Your component name that you are testing in camelCase? (e.g. amazingThing)',
        validate: input => {
          if (input.trim()) {
            return true;
          }
          return 'Name is required';
        },
      },
      {
        type: 'directory',
        name: 'path',
        message: 'What directory do you want your test in?',
        basePath: path.join(process.cwd(), 'src'),
      },
    ],
    actions: [
      {
        type: 'add',
        path: `${process.cwd()}/src/{{path}}/{{dashCase componentName}}.test.js`,
        templateFile: './templates/component/my-component-test.js',
      },
    ],
  });

  // Generates a new patchwork component in its own top-level folder.
  plop.setGenerator('component', {
    prompts: [
      {
        type: 'input',
        name: 'componentName',
        message: "Your new component's name in camelCase? (e.g. amazingThing)",
        validate: input => {
          if (input.trim()) {
            return true;
          }
          return 'Name is required';
        },
      },
      {
        type: 'directory',
        name: 'path',
        message: 'What directory do you want your file in?',
        basePath: path.join(process.cwd(), 'src'),
      },
    ],
    actions: [
      {
        type: 'add',
        path: `${process.cwd()}/src/{{path}}/{{dashCase componentName}}/index.js`,
        templateFile: './templates/component/index.js',
      },
      {
        type: 'add',
        path: `${process.cwd()}/src/{{path}}/{{dashCase componentName}}/{{dashCase componentName}}.css-rules.js`,
        templateFile: './templates/component/my-component.css-rules.js',
      },
      {
        type: 'add',
        path: `${process.cwd()}/src/{{path}}/{{dashCase componentName}}/{{dashCase componentName}}.proptypes.js`,
        templateFile: './templates/component/my-component.proptypes.js',
      },
      {
        type: 'add',
        path: `${process.cwd()}/src/{{path}}/{{dashCase componentName}}/{{dashCase componentName}}.story.js`,
        templateFile: './templates/component/my-component.story.js',
      },
      {
        type: 'add',
        path: `${process.cwd()}/src/{{path}}/{{dashCase componentName}}/{{constantCase componentName}}.mkd`,
        templateFile: './templates/component/MY_COMPONENT.mkd',
      },
      {
        type: 'add',
        path: `${process.cwd()}/src/{{path}}/{{dashCase componentName}}/{{dashCase componentName}}.test.js`,
        templateFile: './templates/component/my-component-test.js',
      },
    ],
  });
};
