/* 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.setHelper('toStoryIdPrefix', (item) =>
    (item || '').replace(/\/$/, '').replace(/\//g, '-').toLowerCase(),
  );

  plop.setHelper('relativeToSrc', (item) =>
    item.split('/').reduce((relativePath) => `../${relativePath}`, '../'),
  );

  plop.setHelper('relativeToRoot', (item) =>
    item.split('/').reduce((relativePath) => `../${relativePath}`, '../../'),
  );

  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',
        skipIfExists: true,
      },
      {
        type: 'add',
        path: `${process.cwd()}/src/{{path}}/{{dashCase componentName}}/{{dashCase componentName}}.component.js`,
        templateFile: './templates/component/my-component.component.js',
        skipIfExists: true,
      },
      {
        type: 'add',
        path: `${process.cwd()}/src/{{path}}/{{dashCase componentName}}/{{dashCase componentName}}.css-rules.js`,
        templateFile: './templates/component/my-component.css-rules.js',
        skipIfExists: true,
      },
      {
        type: 'add',
        path: `${process.cwd()}/src/{{path}}/{{dashCase componentName}}/{{dashCase componentName}}.proptypes.js`,
        templateFile: './templates/component/my-component.proptypes.js',
        skipIfExists: true,
      },
      {
        type: 'add',
        path: `${process.cwd()}/src/{{path}}/{{dashCase componentName}}/{{dashCase componentName}}.stories.js`,
        templateFile: './templates/component/my-component.stories.js',
        skipIfExists: true,
      },
      {
        type: 'add',
        path: `${process.cwd()}/src/{{path}}/{{dashCase componentName}}/{{dashCase componentName}}.mdx`,
        templateFile: './templates/component/my-component.mdx',
        skipIfExists: true,
      },
      {
        type: 'add',
        path: `${process.cwd()}/src/{{path}}/{{dashCase componentName}}/{{dashCase componentName}}.test.js`,
        templateFile: './templates/component/my-component-test.js',
        skipIfExists: true,
      },
    ],
  });
};
