const { chalk, clipboardy, address } = require('@umijs/utils');
class DevCompileDonePlugin {
  constructor(opts) {
    this.opts = opts;
  }

  apply(compiler) {
    let isFirstCompile = true;

    compiler.hooks.done.tap('DevFirstCompileDone', stats => {
      if (stats.hasErrors()) {
        // make sound
        if (process.env.SYSTEM_BELL !== 'none') {
          process.stdout.write('\x07');
        }
        this.opts.onCompileFail && this.opts.onCompileFail({
          stats,
        });
        return;
      }

      if (isFirstCompile) {
        const lanIp = address.ip();
        const protocol = this.opts.https ? 'https' : 'http';
        const hostname =
          this.opts.hostname === '0.0.0.0' ? 'localhost' : this.opts.hostname;
        const localUrl = `${protocol}://${hostname}:${this.opts.port}`;
        const lanUrl = `${protocol}://${lanIp}:${this.opts.port}`;
        let copied = '';
        try {
          clipboardy.writeSync(localUrl);
          copied = chalk.dim('(copied to clipboard)');
        } catch (e) {
          copied = chalk.red(`(copy to clipboard failed)`);
        }

        console.log();
        console.log(
          [
            `  App running at:`,
            `  - Local:   ${chalk.cyan(localUrl)} ${copied}`,
            lanUrl && `  - Network: ${chalk.cyan(lanUrl)}`,
          ]
            .filter(Boolean)
            .join('\n'),
        );
      }


      this.opts.onCompileDone && this.opts.onCompileDone({
          isFirstCompile,
          stats,
      });
      if(isFirstCompile) {
          isFirstCompile = false;
          // process.send({
          //     type: 'DONE'
          // })
      }
    });
  }
}

module.exports = DevCompileDonePlugin;