import boxen, { type Options as BoxenOptions } from 'boxen';
import dayjs, { type Dayjs } from 'dayjs';
import duration from 'dayjs/plugin/duration';
import gradientString from 'gradient-string';

import { logOutputSize, wrapperEnv } from './utils';

dayjs.extend(duration);

const boxenOptions: BoxenOptions = {
  padding: 0.94,
  borderColor: 'cyan',
  borderStyle: 'round',
  textAlignment: 'left',
};

/* 输出日志信息 */
const printLogMessage = (VITE_PORT: number) => {
  return gradientString('cyan', 'magenta').multiline(
    `欢迎使用此项目，项目访问地址如下：
http://localhost:${VITE_PORT}`
  );
};

export const viteConsoleLog = (mode: string) => {
  const { VITE_PORT } = wrapperEnv(mode);

  let config: { command: string };
  let startTime: Dayjs;
  let endTime: Dayjs;
  return {
    name: 'vite:buildInfo',
    configResolved(resolvedConfig) {
      config = resolvedConfig;
    },
    buildStart() {
      console.log(boxen(printLogMessage(VITE_PORT), boxenOptions));
      if (config.command === 'build') {
        startTime = dayjs(new Date());
      }
    },
    closeBundle() {
      if (config.command === 'build') {
        endTime = dayjs(new Date());
        const format = dayjs.duration(endTime.diff(startTime)).format('mm分ss秒');

        console.log(
          boxen(
            gradientString('cyan', 'magenta').multiline(
              `🎉 恭喜打包完成（总用时${format}）打包大小（${logOutputSize()}）`
            ),
            boxenOptions
          )
        );
      }
    },
  };
};
