import request from '../utils/request.mjs';
import { initFields } from '../utils/field.mjs';
import btnList from '../example/btn.mjs';
import allRoutes, { RoutesTitle } from '../example/routes.mjs';
import fs from 'fs';

export default {
  prompts: [],
  actions: (prompts) => {
    return [
      // function () {
      //   fs.rmSync(`src/router/routes`, { recursive: true, force: true })
      //   return `-- \\src\\router\\routes`
      // },
      // function () {
      //   fs.rmSync(`src/pages`, { recursive: true, force: true })
      //   return `-- \\src\\pages`
      // },
    ].concat(...Object.keys(allRoutes).map(rKey => {
      const routes = allRoutes[rKey]
      const RoutePlopList = []
      return [
        {
          type: 'add',
          path: `src/router/routes/${rKey}.ts`,
          templateFile: 'plop/template/routes.ts.hbs',
          data: {
            title: RoutesTitle[rKey],
            routes: JSON.stringify(routes, (key, value) => {
              if (['plop'].includes(key)) {
                RoutePlopList.push(value)
                return undefined
              }
              return value
            }, 2).replaceAll(/"___|___"/g, '').replaceAll("~/pages", `~/pages/${rKey}`)
          }
        },
        ...RoutePlopList.map(RoutePlop => ({
          type: 'add',
          path: `src/pages/${rKey}/${RoutePlop.filePath}.vue`,
          templateFile: `plop/template/${RoutePlop.template}.vue.hbs`,
          data: async () => {
            console.log('\x1b[33m%s\x1b[0m', 'RoutePlop :>> ', RoutePlop.template, RoutePlop.filePath, RoutePlop.props);
            switch (RoutePlop.template) {
              case 'index': {
                const res = await request(RoutePlop.url + '/index')
                console.log('> request ', RoutePlop.url + '/index', ' status: ', res.data.code);
                const fields = initFields(res.data)
                return {
                  url: RoutePlop.url,
                  props: RoutePlop.props,
                  ...btnList,
                  ...fields,
                  detailName: RoutePlop.routeName.replaceAll('INDEX', 'DETAIL'),
                  // start from aid, then bid;cid....
                  detailParam: String.fromCodePoint(97 + (RoutePlop.props?.length ?? 0)) + 'id'
                }
              }
              case 'tab': {
                console.log('tab_list :>> ', RoutePlop.children);
                return {
                  tab_list: RoutePlop.children
                }
              }
              case 'detail': {
                const res = await request(RoutePlop.url + '/index')
                const fields = initFields(res.data)

                return {
                  url: RoutePlop.url,
                  props: RoutePlop.props,
                  detailParam: RoutePlop.props.at(-1),
                  ...fields
                }
              }
              default: {
                return {}
              }
            }
          }
        }))
      ]
    }))


  }
}

