/**
 * {inline,para,full}
 * @param       {[type]} status [description]
 * @constructor
 */

function computeStyle(selection, el) {
  let inline,
    para,
    full;
  if (selection.type === 0) {
    // let tmp = selection.select.commonAncestorContainer;
    // if (tmp.nodeName !== '#text') {
    //   // inline = inlineStyle(tmp);
    //   // judgeFont(selection.origin);
    // } else {
    //   // inline = inlineStyle(tmp.parentNode);
    // }
    para = paraStyle(selection.parent);
  } else {
    let paras = []
    paras.push(paraStyle(selection.select.startContainer));
    paras.push(paraStyle(selection.select.endContainer));
    let next = selection.select.startContainer.nextElementSibling;
    while(next&&next!==selection.select.endContainer){
      paras.push(paraStyle(next));
      next = next.nextElementSibling;
    }
  }
  if(selection.range.collapsed){
    inline = judgeFont(selection.wraped,true);
  }else{
    inline = judgeFont(selection.wraped);
  }
  full = paraStyle(el);
  console.log(inline, para, full);
  return {inline, para, full};
}

function inlineStyle(el) {
  if (el && el.style) {
    let {
      color,
      fontSize,
      lineHeight,
      backgroundColor,
      fontStyle,
      fontWeight,
      textDecoration
    } = window.getComputedStyle(el);
    return {
      color,
      fontSize,
      lineHeight,
      backgroundColor,
      fontStyle,
      fontWeight,
      textDecoration
    };
  }
}

function paraStyle(el) {
  let {color, fontSize, lineHeight} = window.getComputedStyle(el);
  return {color, fontSize, lineHeight} //,marginTop,marginLeft,marginRight,marginBottom,paddingTop,paddingLeft,paddingRight,paddingBottom};
}

function judgeFont(el,collapsed) {
  let sizes = [];
  let colors = [];
  let bgColors = [];
  let length = collapsed?1:el.innerText?el.innerText.length:[...el.childNodes].reduce((f,e)=>{
    console.log(e);
    f+=e.innerText?e.innerText.length:e.data?e.data.length:0;
    return f;
  },0);
  let map ={
    'B':0,
    'I':0,
    'U':0,
    'S':0,
    'SUB':0,
    'SUP':0,
  }
  if (el.querySelectorAll) {
    [...el.querySelectorAll('*')].forEach(e => {
      sizes.push(e.style.fontSize);
      colors.push(e.style.color);
      bgColors.push(e.style.backgroudColor);
      map[e.nodeName]+=collapsed?1:e.innerText.length;
    })
  }
  return {
    b:length === map['B'],
    i:length === map['I'],
    u:length === map['U'],
    s:length === map['S'],
    sb:length === map['SUB'],
    sp:length === map['SUP'],
    sizes:Array.from(new Set(sizes)),
    colors:Array.from(new Set(colors)),
    bgColors:Array.from(new Set(bgColors)),
  };
}

// function judgeStyle(type, el) {
//   [...el.querySelectorAll(type)].forEach(e => e.remove());
//   return el.innerText === '';
// }

export default {
  computeStyle
}

@@ -1499,16 +1499,27 @@
 neHeight
+, textAlign
 %7D = wind
@@ -1580,16 +1580,27 @@
 neHeight
+, textAlign
 %7D //,mar

add textAlign to status