import { Space } from 'antd';
import _ from 'lodash';
import { useMemo } from 'react';
import { flatAuditList } from '../AuditList/util';
import PackageCard from './card';

const treeSet = (tree: Record<string, any>[], o: Record<string, any>) => {
  const run = (l: Record<string, any>[]) => {
    return l.map((item) => {
      if (item.children && item.children.length > 0) {
        item.children = run(item.children);
      }
      return { ...item, ..._.cloneDeep(o) };
    });
  };
  return run(tree);
};

export type GroupType = {
  feature: any[];
  option: any[];
  policy: any[];
  role: any[];
  city: any[];
};

export interface PackageListProps {
  applyList?: any[];
}

const PackageList = (props: PackageListProps) => {
  const { applyList } = props;

  const group = useMemo<GroupType>(() => {
    const o = {
      feature: [],
      option: [],
      policy: [],
      role: [],
      city: [],
    };

    if (!Array.isArray(applyList)) return o;

    // 分组处理器
    return applyList.reduce((pre: GroupType, cur: Record<string, any>) => {
      const { auditList, appInfo } = cur ?? {};

      if (!Array.isArray(auditList)) return pre;

      if (auditList[0]?.bpmAuditId) {
        // bpm
        const { feature, option, policy, role, city } = flatAuditList(auditList, (data) => ({
          ...data,
          appId: appInfo?.id,
          appName: appInfo?.name,
        }));
        pre.feature.push(...feature);
        pre.option.push(...option.map((v) => ({ ...v, children: v.optionTree })));
        pre.policy.push(...policy);
        pre.role.push(...role);
        pre.city.push(...city);
        return pre;
      } else {
        // uac
        return auditList.reduce((top: GroupType, audit: any) => {
          const {
            feature,
            option,
            policy,
            role,
            city,
            // 审批属性
            auditType,
            bpmCanAudit,
            bpmAuditId,
            bpmAuditNode,
          } = audit ?? {};
          const s = {
            auditType,
            bpmCanAudit,
            bpmAuditId,
            bpmAuditNode,
            appId: appInfo?.id,
            appName: appInfo?.name,
          };
          top.feature.push(...treeSet(feature ?? [], s));
          top.option.push(
            ...treeSet(option?.map((v: any) => ({ ...v, children: v.optionTree })) ?? [], s),
          );
          top.policy.push(...treeSet(policy ?? [], s));
          top.role.push(...treeSet(role ?? [], s));
          top.city.push(...treeSet(city ?? [], s));
          return top;
        }, pre);
      }
    }, o);
  }, [applyList]);

  return (
    <Space direction={'vertical'} size={'large'} style={{ width: '100%', padding: '0 40px' }}>
      {group.feature.length > 0 && <PackageCard dataSource={group.feature} type={'feature'} />}

      {group.option.length > 0 && <PackageCard dataSource={group.option} type={'option'} />}

      {group.policy.length > 0 && <PackageCard dataSource={group.policy} type={'policy'} />}

      {group.role.length > 0 && <PackageCard dataSource={group.role} type={'role'} />}

      {group.city.length > 0 && <PackageCard dataSource={group.city} type={'city'} />}
    </Space>
  );
};

export default PackageList;
