import { printHelp } from './help.mjs';
import { AbortError } from './packages/events/build/esnext/abort/AbortError.esnext.mjs';
import parseArguments from './node_modules/.pnpm/arg@5.0.2/node_modules/arg/index.mjs';
import stripIndent from './node_modules/.pnpm/common-tags@1.8.2/node_modules/common-tags/es/stripIndent/stripIndent.mjs';
import './node_modules/.pnpm/common-tags@1.8.2/node_modules/common-tags/es/TemplateTag/TemplateTag.mjs';
import './node_modules/.pnpm/common-tags@1.8.2/node_modules/common-tags/es/commaLists/commaLists.mjs';
import './node_modules/.pnpm/common-tags@1.8.2/node_modules/common-tags/es/commaListsAnd/commaListsAnd.mjs';
import './node_modules/.pnpm/common-tags@1.8.2/node_modules/common-tags/es/commaListsOr/commaListsOr.mjs';
import './node_modules/.pnpm/common-tags@1.8.2/node_modules/common-tags/es/html/html.mjs';
import './node_modules/.pnpm/common-tags@1.8.2/node_modules/common-tags/es/safeHtml/safeHtml.mjs';
import './node_modules/.pnpm/common-tags@1.8.2/node_modules/common-tags/es/oneLine/oneLine.mjs';
import './node_modules/.pnpm/common-tags@1.8.2/node_modules/common-tags/es/oneLineTrim/oneLineTrim.mjs';
import './node_modules/.pnpm/common-tags@1.8.2/node_modules/common-tags/es/oneLineCommaLists/oneLineCommaLists.mjs';
import './node_modules/.pnpm/common-tags@1.8.2/node_modules/common-tags/es/oneLineCommaListsOr/oneLineCommaListsOr.mjs';
import './node_modules/.pnpm/common-tags@1.8.2/node_modules/common-tags/es/oneLineCommaListsAnd/oneLineCommaListsAnd.mjs';
import './node_modules/.pnpm/common-tags@1.8.2/node_modules/common-tags/es/inlineLists/inlineLists.mjs';
import './node_modules/.pnpm/common-tags@1.8.2/node_modules/common-tags/es/oneLineInlineLists/oneLineInlineLists.mjs';
import './node_modules/.pnpm/common-tags@1.8.2/node_modules/common-tags/es/stripIndents/stripIndents.mjs';
import { prompt } from './packages/cli-kit/build/esnext/prompt.esnext.mjs';
import { bold } from './node_modules/.pnpm/colorette@2.0.19/node_modules/colorette/index.mjs';

const VALID_PROJECT_KINDS = new Set(['app', 'package', 'module', 'server']);
run().catch(error => {
  if (AbortError.test(error)) return;
  console.error(error);
  process.exitCode = 1;
});
async function run() {
  const permissiveArgs = parseArguments({
    '--help': Boolean,
    '-h': '--help',
    '--package-manager': String
  }, {
    permissive: true
  });
  const firstArgument = permissiveArgs._[0]?.toLowerCase();
  if (firstArgument != null && !firstArgument.startsWith('-') && !VALID_PROJECT_KINDS.has(firstArgument)) {
    // TODO: show help with error message
    process.exitCode = 1;
    return;
  }
  let kind = firstArgument == null || !VALID_PROJECT_KINDS.has(firstArgument) ? undefined : firstArgument;
  const header = stripIndent`
      🧵 ${bold('quilt create')}
    `;
  console.log(header);
  console.log();
  if (permissiveArgs['--help'] && !kind) {
    printHelp({
      packageManager: permissiveArgs['--package-manager']?.toLowerCase()
    });
    return;
  }
  if (kind == null) {
    kind = await prompt({
      type: 'select',
      message: 'What kind of project would you like to create?',
      choices: [{
        title: 'App',
        value: 'app'
      }, {
        title: 'Module',
        value: 'module'
      }, {
        title: 'Package',
        value: 'package'
      }, {
        title: 'Server',
        value: 'server'
      }]
    });
  }
  switch (kind) {
    case 'app':
      {
        const {
          createApp
        } = await import('./app.mjs');
        await createApp();
        break;
      }
    case 'module':
      {
        const {
          createModule
        } = await import('./module.mjs');
        await createModule();
        break;
      }
    case 'package':
      {
        const {
          createProject
        } = await import('./package.mjs');
        await createProject();
        break;
      }
    case 'server':
      {
        const {
          createServer
        } = await import('./server.mjs');
        await createServer();
        break;
      }
  }
}
