'use strict';

var Clipboard = require('clipboard');
var index = require('../message/index.js');

const useClipboard = (opts) => {
  const appendToBody = opts?.appendToBody === void 0 ? true : opts.appendToBody;
  return {
    toClipboard(text, container) {
      return new Promise((resolve, reject) => {
        const fakeEl = document.createElement("button");
        const clipboard = new Clipboard(fakeEl, {
          text: () => text,
          action: () => "copy",
          container: container !== void 0 ? container : document.body
        });
        clipboard.on("success", (e) => {
          clipboard.destroy();
          resolve(e);
        });
        clipboard.on("error", (e) => {
          clipboard.destroy();
          reject(e);
        });
        if (appendToBody) document.body.appendChild(fakeEl);
        fakeEl.click();
        if (appendToBody) document.body.removeChild(fakeEl);
      });
    }
  };
};
const handleCopy = async (str, options) => {
  const { toClipboard } = useClipboard();
  try {
    await toClipboard(str);
    index.GmMessage.success(options?.successMessage ?? "\u590D\u5236\u6210\u529F");
  } catch (error) {
    console.error(error);
    index.GmMessage.error(options?.errorMessage ?? "\u590D\u5236\u5931\u8D25");
  }
};

exports.handleCopy = handleCopy;
exports.useClipboard = useClipboard;
//# sourceMappingURL=method.js.map
