export function getClickPoint (event) {
  let pageX = 0;
  let pageY = 0;
  if (event.touches && event.touches.length) {
    const touch = event.touches[0];
    ({ pageX, pageY } = touch);
  } else if (event.changedTouches) {
    const touch = event.changedTouches[0];
    ({ pageX, pageY } = touch);
  } else {
    ({ pageX, pageY } = event);
  }
  return {
    pageX,
    pageY
  }
}

export function getAbsoluteCoordinate (ele, event) {
  const { pageX, pageY } = getClickPoint(event)

  const rect = ele.getBoundingClientRect();
  const offsetX = window.pageXOffset || window.scrollX || 0;
  const offsetY = window.pageYOffset || window.scrollY || 0;

  const coordinate = {
    x: pageX - rect.left - offsetX,
    y: pageY - rect.top - offsetY,
    width: rect.width - offsetX,
    height: rect.height - offsetY
  };

  return coordinate
}

export function createSvg (type, attribute, style) {
  const svgEle = document.createElementNS("http://www.w3.org/2000/svg", type);
  if (attribute) {
    for (let key in attribute) {
      svgEle.setAttribute(key, attribute[key])
    }
  }
  if (style) {
    for (let key in style) {
      svgEle.style[key] = style[key]
    }
  }
  return svgEle
}

export function getActualPoint (point, viewport) {
  const { scale: s } = viewport
  let x, y;
  x = point.x * s;
  y = point.y * s;
  return {
    x,
    y,
  };
}

export function getInitialPoint (point, viewport, s) {
  let x, y;
  x = point.x / s;
  y = point.y / s; 
  return {
    x,
    y,
  };
}

const TO_FIXED_PRECISION = /(\s?[A-Z]?,?-?[0-9]*\.[0-9]{0,2})(([0-9]|e|-)*)/g;

function med(A, B) {
  return [(A[0] + B[0]) / 2, (A[1] + B[1]) / 2];
}

export function getSvgPathFromStroke(points) {
  if (!points.length) {
    return "";
  }

  const max = points.length - 1;

  return points
    .reduce(
      (acc, point, i, arr) => {
        if (i === max) {
          acc.push(point, med(point, arr[0]), "L", arr[0], "Z");
        } else {
          acc.push(point, med(point, arr[i + 1]));
        }
        return acc;
      },
      ["M", points[0], "Q"],
    )
    .join(" ")
    .replace(TO_FIXED_PRECISION, "$1");
}

export const getHtmlToText = (el) => {
  return formatText(replaceToBreak(getHtml(el)));
}

export const formatText = (text) => {
  return text.replace(/\<br\/\>/gi, '\n').replace(/\<br\>/gi, '\n').replace(/&nbsp;/g, ' ').replace(/&amp;/g, '&').replace(/<\/?[^>]*>/g,'').replace(/&lt;/g, '<').replace(/&gt;/g, '>')
}
/**
* 获取HTML
*/
const getHtml = (el) => {
  return el.innerHTML
}
/**
* 替换DIV到换行符
* @returns {string}
*/
const replaceToBreak = (html) => {
  html = String(html).replace(/<\/div>/gi, '');
  html = html.replace(/<div>(<br>)?(<br\/>)?/gi, '\n');
  html = html.replace(/<br>|<br\/>/gi, '\n');
  return html;
}

export const keepLastIndex = (element) => {
  if (window.getSelection) {
    var range = window.getSelection();
    range.selectAllChildren(element);
    range.collapseToEnd(); //光标移至最后
  } else if (document.selection) {
    var range = document.selection.createRange();
    range.moveToElementText(element);
    range.collapse(false); //光标移至最后
    range.select();
  }
}

export const createElement = (el, style, attribute) => {
  const element = document.createElement(el);
  if (attribute) {
    for (let key in attribute) {
      element.setAttribute(key, attribute[key]);
    }
  }
  if (style) {
    for (let key in style) {
      element.style[key] = style[key];
    }
  }
  return element;
}

/**
 * Insert the plain text into the selected element when pasteing.
 * @param {*} event - Paste Event
 */
export const pastePlainText = (event) => {
  event.preventDefault()

  var text = (event.originalEvent || event).clipboardData.getData('text')
  if (!text) return
  const selection = window.getSelection()
  if (!selection.rangeCount) return
  selection.deleteFromDocument()
  selection.getRangeAt(0).insertNode(document.createTextNode(text))
  selection.collapseToEnd()
}
