// 版本间的补丁

import { EventConfigOld, EventConfigNew } from 'models/component.types';
import { generateId, group } from './utils';

// v4.5 events 中从平铺的动作 {..., ...action} 到数组的动作{...,actions:[action, action]}
export function transformExportDataToLowerVersion4dot5(data) {
  const { componentsConfig, panelConfig, ...restData } = data;
  return {
    ...restData,
    componentsConfig: componentsConfig.map((d) => ({
      ...d,
      events: d.events ? JSON.stringify(transformActionsToPlain(JSON.parse(d.events))) : '[]'
    })),
    panelConfig: panelConfig.map((d) => ({
      ...d,
      stateConfig: d.stateConfig.map((o) => transformExportDataToLowerVersion4dot5(o))
    }))
  };
}

function transformActionsToPlain(events) {
  return events.reduce((all, item) => {
    // 如果是新版本的action，则需要拆分成多个自定义事件
    if (item.actions) {
      const { id: eventId, actions, ...rest } = item;
      // fix: 导出容错，出现actions为非数组的脏数据
      if (Array.isArray(actions)) {
        const newEvents = actions.map((d, i) => {
          const { id: actionId, name: actionName, ...restAction } = d;
          return {
            ...rest,
            ...restAction,
            actionId: actionId,
            actionName: actionName,
            id: `${eventId}_${i}`
          };
        });
        return all.concat(newEvents);
      } else {
        return all;
      }
    } else {
      // 如果不是，则原样输出
      return all.concat(item);
    }
  }, []);
}

export function transformImportDataToHigherVersion4dot5(data) {
  const { componentsConfig, panelConfig, ...restData } = data;
  return {
    ...restData,
    componentsConfig: componentsConfig.map((d) => ({
      ...d,
      events: d.events ? JSON.stringify(transformActionsToArray(JSON.parse(d.events))) : '[]'
    })),
    panelConfig: panelConfig.map((d) => ({
      ...d,
      stateConfig: d.stateConfig.map((o) => transformImportDataToHigherVersion4dot5(o))
    }))
  };
}

interface EventConfigWithSign extends EventConfigOld {
  [x: string]: any;
  actionId: string;
  actionName: string;
}

function transformActionsToArray(events) {
  const isTransformedAction = events.some((d) => d.actionId);
  // 如果存在eventId，表示是被拆分过的action，eventId相同的为拆分前是同一个事件
  if (isTransformedAction) {
    const groupByEventId = group<EventConfigWithSign>(events, (d) => d.id.replace(/_(\d)+$/, ''));

    return groupByEventId.reduce((all: any, item) => {
      if (item.key && item.value.length > 0) {
        const [{ name, trigger, conditionType, conditions, reverse }] = item.value;
        return all.concat({
          id: item.key,
          name,
          trigger,
          conditionType,
          conditions,
          reverse,
          actions: item.value.map((d) => {
            const { id, name, trigger, conditionType, conditions, reverse, ...rest } = d;
            return {
              ...rest,
              id: `action_${generateId()}`,
              name: d.actionName,
              action: d.action,
              rotate: d.rotate,
              actionData: d.actionData,
              animation: d.animation,
              component: d.component,
              componentScope: d.componentScope,
              componentConfig: d.componentConfig,
              scale: d.scale,
              stateId: d.stateId,
              translate: d.translate,
              unmount: d.unmount,
              actionDataFn: d.actionDataFn
            };
          })
        });
      } else {
        return all.concat(item.value);
      }
    }, []);
  } else {
    return events;
  }
}
