import { loadScript } from '@/utils/commons';
import { BUILD_VERSION, BUILD_DATE } from '@/config'

export const loadPageSpy = (title) => {

  if (import.meta.env.DEV || window.$pageSpy) return

  const PageSpyConfig = { api: 'page-spy.mycht.cn', project: 'Sales CRM', title: title + '(v' + BUILD_VERSION + ')', autoRender: false, offline: true,  };

  const PageSpySrc = [
    'https://page-spy.mycht.cn/page-spy/index.min.js'+`?${BUILD_DATE}`,
    'https://page-spy.mycht.cn/plugin/data-harbor/index.min.js'+`?${BUILD_DATE}`,
    'https://page-spy.mycht.cn/plugin/rrweb/index.min.js'+`?${BUILD_DATE}`,
  ];
  Promise.all(PageSpySrc.map((src) => loadScript(src))).then(() => {

    // 注册插件
    window.$harbor = new DataHarborPlugin();
    window.$rrweb = new RRWebPlugin();
    [window.$harbor, window.$rrweb].forEach(p => {
      PageSpy.registerPlugin(p)
    })
    window.$pageSpy = new PageSpy(PageSpyConfig);

    // PageSpy.registerPlugin(new DataHarborPlugin());
    // PageSpy.registerPlugin(new RRWebPlugin());
    // 实例化 PageSpy
    // window.$pageSpy = new PageSpy({ api: 'page-spy.mycht.cn', project: 'Sales CRM', title: title + '(v' + BUILD_VERSION + ')', autoRender: false, offline: true,  });
    console.log('[PageSpy]', window.$pageSpy.version);
  });
};
