import * as FileSizeFormat from "filesize";
import * as moment from "moment";

export function SetVariableFormat(string, format) {
  if (!string) return string;

  switch (format) {
    case "date":
      if (string) {
        return moment(string - 10)
          .startOf("minute")
          .fromNow();
      } else {
        return "-";
      }
    case "size":
      return FileSizeFormat(string);
    case "truncate":
      return truncate(string, 60);
    default:
      return string;
  }
}

export function truncate(fullStr, strLen, separator) {
  if (fullStr.length <= strLen) return fullStr;

  separator = separator || "...";

  var sepLen = separator.length,
    charsToShow = strLen - sepLen,
    frontChars = Math.ceil(charsToShow / 2),
    backChars = Math.floor(charsToShow / 2);

  return (
    fullStr.substr(0, frontChars) +
    separator +
    fullStr.substr(fullStr.length - backChars)
  );
}

/**
 * This first variant without {@link node} does not work for the pop-up dialog, 
 * so the variant with {@link node} should be preferred

 * @param {*} text 
 * @param {*} node 
 */
export function setClipboardText(text, node) {
  if (!node) {
    var id = "mycustom-clipboard-textarea-hidden-id";
    var existsTextarea = document.getElementById(id);

    if (!existsTextarea) {
      var textarea = document.createElement("textarea");
      textarea.id = id;
      // Place in top-left corner of screen regardless of scroll position.
      textarea.style.position = "fixed";
      textarea.style.top = 0;
      textarea.style.left = 0;

      // Ensure it has a small width and height. Setting to 1px / 1em
      // doesn't work as this gives a negative w/h on some browsers.
      textarea.style.width = "1px";
      textarea.style.height = "1px";

      // We don't need padding, reducing the size if it does flash render.
      textarea.style.padding = 0;

      // Clean up any borders.
      textarea.style.border = "none";
      textarea.style.outline = "none";
      textarea.style.boxShadow = "none";

      // Avoid flash of white box if rendered for any reason.
      textarea.style.background = "transparent";
      document.querySelector("body").appendChild(textarea);
      existsTextarea = document.getElementById(id);
    }

    existsTextarea.value = text;
    existsTextarea.select();

    try {
      document.execCommand("copy");
    } catch (err) {
      console.error(err);
    }
  } else {
    const selection = window.getSelection();
    const range = document.createRange();
    range.selectNodeContents(node);
    selection.removeAllRanges();
    selection.addRange(range);
    document.execCommand("copy");
    selection.removeAllRanges();
  }
}