/**
 * 返回群发原始数据
 * @param data 
 */
export const getRobotSendData = (data: { [x: string]: any }) => {
    const { groupSendName, strategyList } = data
    return {
        groupSendName,
        strategyList: strategyList.map(item => {
            const { taskDetail, ...its } = item
            return {
                ...its,
                sendData: taskDetail.map(td => {
                    const { externalUserFilterName, externalUserFilter, robotCorpUser, ...itsd } = td
                    const detail: { [x: string]: any } = {
                        ...itsd,
                        sendGroupName: externalUserFilterName,
                        chatCorpId: itsd?.corpGroupChat?.corpId
                    }
                    if (externalUserFilter) {
                        detail.externalUserType = 'specify'
                        const { configName, ...configContent } = externalUserFilter
                        detail.externalUserFilter = {
                            configName,
                            configContent
                        }
                    } else {
                        detail.externalUserType = 'all'
                    }
                    if (robotCorpUser) {
                        detail.robotCorpId = robotCorpUser.corpId
                        detail.robotCorpUser = [robotCorpUser]
                    }
                    return detail
                })
            }
        })
    }
}


interface CorpGroupChat {
  chatId: string;
  chatName: string;
  corpId: string;
  corpName: string;
}

interface InputItem {
  chatCorpId: string;
  corpGroupChat: CorpGroupChat;
}

interface OutputStructure {
  [key: string]: CorpGroupChat[][];
}

export function transformData(input: Record<string, InputItem>): OutputStructure {
  const output: OutputStructure = {};
  const groupMap = new Map<string, Map<string, CorpGroupChat[]>>();

  // 遍历输入数据并分类
  Object.entries(input).forEach(([key, value]) => {
    const [prefix, group, _] = key.split('_');
    if (!groupMap.has(prefix)) {
      groupMap.set(prefix, new Map<string, CorpGroupChat[]>());
    }
    const groupData = groupMap.get(prefix)!;
    
    if (!groupData.has(group)) {
      groupData.set(group, []);
    }
    groupData.get(group)!.push(value.corpGroupChat);
  });

  // 转换为输出格式
  groupMap.forEach((groups, prefix) => {
    output[prefix] = Array.from(groups.values());
  });

  return output;
}