'use strict';

Object.defineProperty(exports, '__esModule', { value: true });

var toggleSelection = require('./toggle-selection.cjs');

const clipboardToIE11Formatting = {
  "text/plain": "Text",
  "text/html": "Url",
  "default": "Text"
};
const defaultMessage = "Copy to clipboard: #{key}, Enter";
function format(message) {
  const copyKey = `${/mac os x/i.test(navigator.userAgent) ? "\u2318" : "Ctrl"}+C`;
  return message.replace(/#{\s*key\s*}/g, copyKey);
}
function copy(text, options) {
  let message;
  let reselectPrevious;
  let range;
  let selection;
  let mark;
  let success = false;
  if (!options)
    options = {};
  const debug = options.debug || false;
  try {
    reselectPrevious = toggleSelection.default();
    range = document.createRange();
    selection = document.getSelection();
    mark = document.createElement("span");
    mark.textContent = text;
    mark.style.all = "unset";
    mark.style.position = "fixed";
    mark.style.top = 0;
    mark.style.clip = "rect(0, 0, 0, 0)";
    mark.style.whiteSpace = "pre";
    mark.style.webkitUserSelect = "text";
    mark.style.MozUserSelect = "text";
    mark.style.msUserSelect = "text";
    mark.style.userSelect = "text";
    mark.addEventListener("copy", (e) => {
      e.stopPropagation();
      if (options.format) {
        e.preventDefault();
        if (typeof e.clipboardData === "undefined") {
          debug && console.warn("unable to use e.clipboardData");
          debug && console.warn("trying IE specific stuff");
          window.clipboardData.clearData();
          const format2 = clipboardToIE11Formatting[options.format] || clipboardToIE11Formatting.default;
          window.clipboardData.setData(format2, text);
        } else {
          e.clipboardData.clearData();
          e.clipboardData.setData(options.format, text);
        }
      }
      if (options.onCopy) {
        e.preventDefault();
        options.onCopy(e.clipboardData);
      }
    });
    document.body.appendChild(mark);
    range.selectNodeContents(mark);
    selection.addRange(range);
    const successful = document.execCommand("copy");
    if (!successful)
      throw new Error("copy command was unsuccessful");
    success = true;
  } catch (err) {
    debug && console.error("unable to copy using execCommand: ", err);
    debug && console.warn("trying IE specific stuff");
    try {
      window.clipboardData.setData(options.format || "text", text);
      options.onCopy && options.onCopy(window.clipboardData);
      success = true;
    } catch (err2) {
      debug && console.error("unable to copy using clipboardData: ", err2);
      debug && console.error("falling back to prompt");
      message = format("message" in options ? options.message : defaultMessage);
      window.prompt(message, text);
    }
  } finally {
    if (selection) {
      if (typeof selection.removeRange == "function")
        selection.removeRange(range);
      else
        selection.removeAllRanges();
    }
    if (mark)
      document.body.removeChild(mark);
    reselectPrevious();
  }
  return success;
}

exports.default = copy;
//# sourceMappingURL=index.cjs.map
