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

export default {
  prompts: [],
  actions: (prompts) => {

    const routes = allRoutes
    const RoutePlopList = []
    return [
      {
        type: 'add',
        path: 'src/router/routes.ts',
        templateFile: 'plop/template/routes.ts.hbs',
        data: {
          routes: JSON.stringify(routes, (key, value) => {
            if (['plop'].includes(key)) {
              RoutePlopList.push(value)
              return undefined
            }
            return value
          }, 2).replaceAll(/"___|___"/g, '')
        }
      },
      function () {
        fs.rmSync(`src/pages`, { recursive: true, force: true })
        return `-- \\src\\pages`
      },
      ...RoutePlopList.map(RoutePlop => ({
        type: 'add',
        path: `src/pages/${RoutePlop.filePath}.vue`,
        templateFile: `plop/template/${RoutePlop.template}.vue.hbs`,
        data: async () => {
          console.log('RoutePlop. :>> ', RoutePlop.url);
          switch (RoutePlop.template) {
            case 'index': {
              const res = await request(RoutePlop.url + '/index')
              const fields = initFields(res.data)
              return {
                url: RoutePlop.url,
                ...btnList,
                ...fields,
                detailName: RoutePlop.routeName.replaceAll('INDEX', 'DETAIL')
              }
            }
            case 'tab': {
              return {
                tab_list: RoutePlop.children
              }
            }
            case 'detail': {
              const res = await request(RoutePlop.url + '/index')
              const fields = initFields(res.data)

              return {
                url: RoutePlop.url,
                ...fields
              }
            }
            default: {
              return {}
            }
          }
        }
      }))
    ]


  }
}

