export const cardNestStructure = (data) => {
  let childList = {}
  let result = []
  data.forEach(item => {
    if(item.schemas){
      item.schemas = cardNestStructure(item.schemas)
      result.push(item)
    }else if(item.ptype){
      if(Object.keys(item).includes('children')){
        item.children = childList[item.field] || []
      }
      if(item.component === 'CardGroup' && Object.keys(item).includes('componentProps')){
        item.componentProps.options = childList[item.field] || []
      }
      let pushItem = {}
      Object.keys(item).forEach(key => {
        if(key !== 'ptype' && key !== 'pfield'){
          pushItem[key] = item[key]
        }
      })
      if(childList[item.pfield]){
        childList[item.pfield].push(pushItem)
      }else{
        childList[item.pfield] = [pushItem]
      }

    }else if(item.component === 'CardGroup'){
      item.componentProps.options = childList[item.field] || []
      result.push(item)
    }else if(item.component === 'Card'){
      item.columns[0].children = childList[item.field] || []
      result.push(item)
    }else if(item.field && item.field.indexOf('use_card') !== -1){
      item.children = childList[item.field] || []
      result.push(item)
    }else{
      result.push(item)
    }
  })
  return result
}
export const changeCardStructure = (data) => {
  let result = [];
  if (data && data.length > 0) {
    data.forEach((item) => {
      if (item.component === 'Tabs') {
        item.componentProps.options = changeCardStructure(item.componentProps.options);
      } else if (item.component === 'CardGroup') {
        item.componentProps.options = changeCardStructure(item.componentProps.options);
        item.componentProps.options.forEach((childItem) => {
          if (childItem.pfield) {
            result.push(childItem);
          } else {
            result.push({ ...childItem, ptype: 'card', pfield: item.field });
          }
        });
        item.componentProps.options = [];
      } else if (item.component === 'Grid' && item.label === '栅格布局') {
        item.columns = changeCardStructure(item.columns);
        item.columns.forEach((childItem) => {
          childItem.children.forEach((col) => {
            result.push({ ...col, ptype: 'gridlayout', pfield: item.field });
          });
        });
      } else if (item.children && !item.field) {
        item.children = changeCardStructure(item.children);
      } else if (item.children && item.field.indexOf('use_card') === -1) {
        item.children = changeCardStructure(item.children);
        item.children = [];
      } else {
        if (['Card'].includes(item.component)) {
          item.columns[0].children = changeCardStructure(item.columns[0].children);
          if (item.component === 'Card' && item.columns[0].children) {
            item.columns[0].children.forEach((childItem) => {
              if (childItem.pfield) {
                result.push(childItem);
              } else {
                result.push({ ...childItem, ptype: 'card', pfield: item.field });
              }
            });
            item.columns[0].children = [];
          }
        } else if (item.field && item.field.indexOf('use_card') !== -1) {
          item.children = changeCardStructure(item.children);
          item.children.forEach((childItem) => {
            if (childItem.pfield) {
              result.push(childItem);
            } else {
              result.push({ ...childItem, ptype: 'card', pfield: item.field });
            }
          });
          item.children = [];
        }
      }
      result.push(item);
    });
    return result;
  } else {
    return data;
  }
};