function _slicedToArray(arr, i) { return _arrayWithHoles(arr) || _iterableToArrayLimit(arr, i) || _unsupportedIterableToArray(arr, i) || _nonIterableRest(); }

function _nonIterableRest() { throw new TypeError("Invalid attempt to destructure non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method."); }

function _unsupportedIterableToArray(o, minLen) { if (!o) return; if (typeof o === "string") return _arrayLikeToArray(o, minLen); var n = Object.prototype.toString.call(o).slice(8, -1); if (n === "Object" && o.constructor) n = o.constructor.name; if (n === "Map" || n === "Set") return Array.from(o); if (n === "Arguments" || /^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(n)) return _arrayLikeToArray(o, minLen); }

function _arrayLikeToArray(arr, len) { if (len == null || len > arr.length) len = arr.length; for (var i = 0, arr2 = new Array(len); i < len; i++) { arr2[i] = arr[i]; } return arr2; }

function _iterableToArrayLimit(arr, i) { var _i = arr == null ? null : typeof Symbol !== "undefined" && arr[Symbol.iterator] || arr["@@iterator"]; if (_i == null) return; var _arr = []; var _n = true; var _d = false; var _s, _e; try { for (_i = _i.call(arr); !(_n = (_s = _i.next()).done); _n = true) { _arr.push(_s.value); if (i && _arr.length === i) break; } } catch (err) { _d = true; _e = err; } finally { try { if (!_n && _i["return"] != null) _i["return"](); } finally { if (_d) throw _e; } } return _arr; }

function _arrayWithHoles(arr) { if (Array.isArray(arr)) return arr; }

function ownKeys(object, enumerableOnly) { var keys = Object.keys(object); if (Object.getOwnPropertySymbols) { var symbols = Object.getOwnPropertySymbols(object); if (enumerableOnly) { symbols = symbols.filter(function (sym) { return Object.getOwnPropertyDescriptor(object, sym).enumerable; }); } keys.push.apply(keys, symbols); } return keys; }

function _objectSpread(target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i] != null ? arguments[i] : {}; if (i % 2) { ownKeys(Object(source), true).forEach(function (key) { _defineProperty(target, key, source[key]); }); } else if (Object.getOwnPropertyDescriptors) { Object.defineProperties(target, Object.getOwnPropertyDescriptors(source)); } else { ownKeys(Object(source)).forEach(function (key) { Object.defineProperty(target, key, Object.getOwnPropertyDescriptor(source, key)); }); } } return target; }

function _defineProperty(obj, key, value) { if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; }

import { jsx } from 'slate-hyperscript';
import escapeHtml from 'escape-html';
import { string } from 'to-style';
import { Text, Node, Element, TextWrappers } from './slate';
export var DEFAULTCOLOR = 'rgb(26, 43, 78)'; // TODO 临时通过始终有一个空格来修复，在空文本-中文输入下失焦的问题

export var EMPTY_DESCENDANT = [{
  type: 'paragraph',
  children: [{
    text: ' '
  }]
}];
var ElementMap = {
  'block-quote': {
    tag: 'blockquote',
    css: {
      fontSize: '14px',
      borderLeft: '2px solid #ddd',
      marginLeft: '0',
      marginRight: '0',
      paddingLeft: '10px',
      color: '#aaaaaa',
      fontStyle: 'italic'
    }
  },
  'bulleted-list': {
    tag: 'ul',
    css: {
      fontSize: '14px'
    }
  },
  'heading-one': {
    tag: 'h1',
    css: {
      fontSize: '18px',
      marginBottom: '8px',
      color: DEFAULTCOLOR
    }
  },
  'heading-two': {
    tag: 'h2',
    css: {
      fontSize: '16px',
      marginBottom: '8px',
      color: DEFAULTCOLOR
    }
  },
  'list-item': {
    tag: 'li',
    css: {
      fontSize: '14px'
    }
  },
  'numbered-list': {
    tag: 'ol',
    css: {
      fontSize: '14px'
    }
  },
  paragraph: {
    tag: 'p',
    css: {
      fontSize: '14px',
      color: 'rgba(26,43,78,0.80)',
      marginBottom: '8px'
    }
  }
};
export function isTextWrapper(node) {
  return Element.isElement(node) && TextWrappers.includes(node.type);
}
export function toStyleString(css) {
  return string(css);
}
export function serializeElement(element) {
  var info = ElementMap[element.type];

  if (isTextWrapper(element)) {
    info.css = _objectSpread(_objectSpread({}, info.css), {}, {
      textAlign: element.textAlign || 'left'
    });
  }

  return info;
}
export function serializeLeaf(leaf) {
  var style = {
    color: leaf.color || DEFAULTCOLOR
  };

  if (style.color === DEFAULTCOLOR) {
    style.color = 'currentcolor';
  }

  if (leaf.bold) {
    style.fontWeight = 'bolder';
  }

  if (leaf.italic) {
    style.fontStyle = 'italic';
  }

  if (leaf.underline) {
    style.textDecoration = 'underline';
  }

  return {
    tag: 'span',
    css: style
  };
}

function serialize(node) {
  if (Text.isText(node)) {
    var _children = escapeHtml(node.text);

    var _serializeLeaf = serializeLeaf(node),
        _tag = _serializeLeaf.tag,
        _css = _serializeLeaf.css;

    return "<".concat(_tag, " style=\"").concat(toStyleString(_css), "\"}>").concat(_children, "</").concat(_tag, ">");
  }

  var children = node.children.map(function (n) {
    return serialize(n);
  }).join('');

  var _serializeElement = serializeElement(node),
      tag = _serializeElement.tag,
      css = _serializeElement.css;

  return "<".concat(tag, " style=\"").concat(toStyleString(css), "\">").concat(children, "</").concat(tag, ">");
}

function deserialize(el) {
  if (el.nodeType === 3) {
    return el.textContent;
  } else if (el.nodeType !== 1) {
    return null;
  }

  var children = Array.from(el.childNodes).map(deserialize);

  if (children.length === 0) {
    children = [{
      text: ''
    }];
  }

  var elProps = {};

  switch (el.nodeName) {
    case 'BODY':
      return jsx('fragment', {}, children);

    case 'BR':
      return '\n';

    case 'BLOCKQUOTE':
      return jsx('element', {
        type: 'block-quote'
      }, children);

    case 'H1':
      return jsx('element', {
        type: 'heading-one',
        textAlign: el.style.textAlign || 'left'
      }, children);

    case 'H2':
      return jsx('element', {
        type: 'heading-two',
        textAlign: el.style.textAlign || 'left'
      }, children);

    case 'LI':
      return jsx('element', {
        type: 'list-item'
      }, children);

    case 'UL':
      return jsx('element', {
        type: 'bulleted-list'
      }, children);

    case 'OL':
      return jsx('element', {
        type: 'numbered-list'
      }, children);

    case 'P':
      return jsx('element', {
        type: 'paragraph',
        textAlign: el.style.textAlign || 'left'
      }, children);

    case 'SPAN':
      if (el.style.fontWeight === 'bolder') {
        elProps.bold = true;
      }

      if (el.style.fontStyle === 'italic') {
        elProps.italic = true;
      }

      if (el.style.textDecoration === 'underline') {
        elProps.underline = true;
      }

      elProps.color = el.style.color || DEFAULTCOLOR;
      return jsx('text', elProps, children);

    default:
      return el.textContent;
  }
}

export function serializeText(nodes) {
  return nodes.map(function (n) {
    return Node.string(n);
  }).join('\n').trim();
}
export function serializeHTML(nodes) {
  if (!serializeText(nodes)) return '';
  return nodes.map(serialize).join('');
}
export function deserializeHTML(html) {
  if (!html) return EMPTY_DESCENDANT;
  var document = new DOMParser().parseFromString(html, 'text/html');
  return deserialize(document.body);
}
var catchSlateFragment = /data-slate-fragment="(.+?)"/m;
export var getSlateFragmentAttribute = function getSlateFragmentAttribute(dataTransfer) {
  var htmlData = dataTransfer.getData('text/html');

  var _ref = htmlData.match(catchSlateFragment) || [],
      _ref2 = _slicedToArray(_ref, 2),
      fragment = _ref2[1];

  return fragment;
};