"use strict";

var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
exports.__esModule = true;
exports.createDataSource = exports.createContents = void 0;
exports.getDataFromPlainText = getDataFromPlainText;
var _extends2 = _interopRequireDefault(require("@babel/runtime/helpers/extends"));
var _parseData = _interopRequireDefault(require("../utils/parse-data"));
var createDataSource = function createDataSource(list, keys, level, prefix) {
  if (keys === void 0) {
    keys = {
      selected: [],
      expanded: {}
    };
  }
  if (level === void 0) {
    level = 0;
  }
  if (prefix === void 0) {
    prefix = '';
  }
  var array = [];
  var group = [];
  var grouping = false;
  var index = 0;
  list.forEach(function (item) {
    switch (item.type) {
      // eslint-disable-next-line no-case-declarations
      case 'node':
        var key = (prefix || level) + "-" + index++;
        if (item.children && item.children.length > 0) {
          item.children = createDataSource(item.children, keys, level + 1, key);
        }
        if (grouping) {
          group.push((0, _extends2["default"])({}, item, {
            key: key
          }));
        } else {
          array.push((0, _extends2["default"])({}, item, {
            key: key
          }));
        }
        if (item.state === 'active') {
          if (item.children && item.children.length > 0) {
            keys.expanded.push(key);
          } else {
            keys.selected.push(key);
          }
        }
        return;
      case 'comment':
        if (group.length > 0) {
          array.push({
            type: 'group',
            value: grouping,
            children: group,
            key: (prefix || level) + "-" + index++
          });
          group = [];
        }
        grouping = item.value;
        return;
      case 'divider':
        if (group.length > 0) {
          array.push({
            type: 'group',
            value: grouping,
            children: group,
            key: (prefix || level) + "-" + index++
          });
          group = [];
        }
        grouping = false;
        array.push({
          type: 'divider',
          key: (prefix || level) + "-" + index++
        });
      default:
    }
  });
  if (group.length > 0) {
    array.push({
      type: 'group',
      value: grouping,
      children: group,
      key: (prefix || level) + "-" + index++
    });
    group = [];
  }
  return array;
};
exports.createDataSource = createDataSource;
var createMenuItem = function createMenuItem(item) {
  if (item.children.length > 0) {
    // return (
    //   <Menu.SubMenu key={item.key} disabled={item.state === 'disabled'} label={item.value ? item.value.filter(({ type }) => type === ContentType.text).map(({ value }) => value).join('') : ''}>
    //     {createContents(item.children)}
    //   </Menu.SubMenu>
    // );
    return {
      componentName: 'SubMenu',
      props: {
        key: item.key,
        disabled: item.state === 'disabled',
        label: item.value ? item.value.filter(function (_ref) {
          var type = _ref.type;
          return type === 'text';
        }).map(function (_ref2) {
          var value = _ref2.value;
          return value;
        }).join('') : ''
      },
      children: createContents(item.children)
    };
  }

  // return <Menu.Item key={item.key} checked={item.state === 'active'} disabled={item.state === 'disabled'} children={item.value.map(({ type, value }, index) => type === 'icon' ? <Icon key={`icon_${index}`} type={value} size="small" style={{ marginRight: '4px' }} /> : value)} />;
  return {
    componentName: 'Menu.Item',
    props: {
      key: item.key,
      checked: item.state === 'active',
      disabled: item.state === 'disabled'
    },
    children: item.value.map(function (_ref3, index) {
      var type = _ref3.type,
        value = _ref3.value;
      return type === 'icon' ? {
        componentName: 'Icon',
        props: {
          disabled: true,
          key: "icon_" + index,
          type: value,
          size: 'small',
          style: {
            marginRight: '4px'
          }
        }
      } : {
        componentName: 'Typography.Text',
        props: {
          children: value,
          style: {
            color: 'inherit'
          }
        }
      };
    })
  };
};
var createContents = function createContents(array) {
  if (array === void 0) {
    array = [];
  }
  return array.map(function (item) {
    if (item.type === 'group' && item.children.length > 0) {
      // return <Menu.Group key={item.key} label={item.value}>{item.children.map(it => createMenuItem(it))}</Menu.Group>;
      return {
        componentName: 'Menu.Group',
        props: {
          key: item.key,
          label: item.value
        },
        children: item.children.map(function (it) {
          return createMenuItem(it);
        })
      };
    }
    if (item.type === 'divider') {
      // return <Menu.Divider key={item.key}/>;
      return {
        componentName: 'Menu.Divider',
        props: {
          key: item.key
        }
      };
    }
    return createMenuItem(item);
  });
};
exports.createContents = createContents;
function getButtonLabel(buttonItem) {
  if (buttonItem.type !== 'node') return {};
  // return buttonItem.value.find(item => item.type === 'text').value;
  // FIXME: 渲染时 JSSlot 有问题，暂时不要 icon
  return {
    label: {
      type: 'JSSlot',
      value: buttonItem.value.map(function (_ref4) {
        var type = _ref4.type,
          value = _ref4.value;
        if (type === 'icon') return {
          componentName: 'Icon',
          props: {
            disabled: true,
            type: value,
            size: 'small',
            style: {
              marginRight: '4px'
            }
          }
        };
        return {
          componentName: 'Typography.Text',
          props: {
            children: value,
            style: {
              color: 'inherit'
            }
          }
        };
      })
    },
    disabled: (buttonItem === null || buttonItem === void 0 ? void 0 : buttonItem.state) === 'disabled'
  };
}
function getDataFromPlainText(value) {
  var keys = {
    selected: [],
    expanded: []
  };
  var list = (0, _parseData["default"])(value, {
    parseContent: true
  });
  var buttonItem = list[0] ? list[0] : {
    value: []
  };
  var _getButtonLabel = getButtonLabel(buttonItem),
    label = _getButtonLabel.label,
    disabled = _getButtonLabel.disabled;
  var dataSource = createDataSource(list[0] ? list[0].children : [], keys);
  var children = createContents(dataSource);
  return {
    label: label,
    disabled: disabled,
    children: children,
    selectedKeys: keys.selected
  };
}

// const _propsValue = ({ shape, level, size, data, ...others}) =>{
//   const list = parseData(data, { parseContent: true });
//   const buttonItem = list[0] ? list[0] : { value: []};
//   const keys = { selected: [], expanded: [] };
//   if (buttonItem.type !== 'node') return null;
//   const label = buttonItem.value.map(({ type, value}) => {
//     if (type === 'icon') return <Icon type={value} />;
//     return value;
//   });

//   return {
//     ...others,
//     size,
//     disabled: buttonItem.state === 'disabled',
//     visible: buttonItem.state === 'active',
//     type: shape === 'ghost' ? 'normal' : level,
//     popupProps: { needAdjust: false, container: node => node },
//     ghost: shape === 'ghost' ? level : false,
//     selectMode: "multiple",
//     menuProps: { openKeys: keys.expanded, style: { textAlign: 'left' } },
//     selectedKeys: keys.selected,
//     label: label
//   };
// };
// export default {
//     name: 'SplitButton',
//     shape: ['normal', 'ghost'],
//     editor: (shape = 'normal') => ({
//         props: [{
//             name: 'level',
//             type: Types.enum,
//             options: shape === 'ghost' ? ['light', 'dark'] : ['normal', 'primary', 'secondary'],
//             default: shape === 'ghost' ? 'light' : 'normal',
//         }, {
//             name: 'size',
//             type: Types.enum,
//             options: ['large', 'medium', 'small'],
//             default: 'medium'
//         }],
//         data: {
//             icon: true,
//             active: true,
//             disable: true,
//             default: 'Edit Document\n\tUndo\n\t*Redo\n\tCut\n\tCopy\n\tPaste'
//         }
//     }),
//     propsValue: _propsValue,
//     adaptor: (args) => {
//         const list = parseData(args.data, { parseContent: true });
//         const keys = { selected: [], expanded: [] };
//         const dataSouce = createDataSource(list[0] ? list[0].children : [], keys);
//         const props = _propsValue(args);
//         return (
//             <SplitButton
//                 {...props}
//             >
//                 {createContents(dataSouce)}
//             </SplitButton>
//         );
//     },
//     demoOptions: (demo) => {
//         const { node = { props: {} } } = demo;
//         const { level, data } = node.props;
//         if (data.indexOf('*') === 0) {
//             demo = {
//                 ...demo,
//                 height: 250
//             };
//         }

//         if (level === 'dark') {
//             demo = {
//                 ...demo,
//                 background: '#333'
//             };
//         } else if(level === 'light') {
//             demo = {
//                 ...demo,
//                 background: 'rgb(235, 236, 240)',
//             };
//         }

//         return demo;
//     }
// };