// This script will be run within the webview itself
// It cannot access the main VS Code APIs directly.
(function () {
  var startIng = false;
  var vscode = acquireVsCodeApi();

  function openDoc() {
    var url = this.getAttribute('data-href');
    vscode.postMessage({ type: 'metawork.openLink', url: url });
  }

  // Handle messages sent from the extension to the webview
  window.addEventListener('message', (event) => {
    var message = event.data;
    switch (message.type) {
      case 'metawork.start': {
        break;
      }
    }
  });

  // 加载完毕后展示dom
  window.addEventListener("load", function () {
    document.body.className = '';
    let userAgent = navigator.userAgent.toLowerCase();
    var isSafari = /safari/.test(userAgent) && !/chrome/.test(userAgent);

    if (isSafari) {
      new Clipboard('.start-button', {
        text: function (trigger) {
          return document.getElementById("shareText").value;
        }
      }).on('success', function (e) {
        e.clearSelection();
      });
    }

    document.querySelector('.start-button').addEventListener('click', () => {
      if (!startIng) {
        vscode.postMessage({ type: 'metawork.start' });
        // 防止重复提交
        startIng = true;
        setTimeout(() => {
          startIng = false;
        }, 5000);
      }
    });

    document.getElementById("btnHelpDoc").addEventListener("click", openDoc);
    document.getElementById("btnLoginDoc").addEventListener("click", openDoc);
  }, false);

})();
