module.exports = {
  init(server, node, opts) {
    server.get(`**${opts.prefix}/api/env`, async (req, res) => {
      res.type('js');

      const [blocklet, config, info] = await Promise.all([
        req.getBlockletInfo(),
        req.getServiceConfig(opts.name),
        req.getNodeInfo(),
      ]);
      const pathPrefix = req.headers['x-path-prefix'] || '/';
      const groupPathPrefix = req.headers['x-group-path-prefix'];
      res.send(`window.env = {
  appId: "${blocklet.did}",
  appName: "${blocklet.name}",
  pathPrefix: "${pathPrefix}",
  apiPrefix: "${pathPrefix.replace(/\/+$/, '')}${opts.prefix}",
  ${groupPathPrefix ? `groupPathPrefix: "${groupPathPrefix}",` : ''}
  webWalletUrl: "${info.webWalletUrl || config.webWalletUrl || opts.webWalletUrl}",
}`);
    });
  },
};
