import React, { useEffect, useState } from 'react';
import FlowEditor from '@/pages/flowEditor/index';
import { useSelector, useDispatch } from 'react-redux';
import { getAppEventList } from '@/api/appEvent';
import { getTopicList, queryEventItemBySceneId } from '@/api/event';
import { updateEventTopicList } from '@/store/ideContainer';

const ApplicationContainer = () => {
  const { info } = useSelector((state: any) => state.ideContainer);
  const [appFlowList, setAppFlowList] = useState([]);
  const dispatch = useDispatch();

  const getAppFlowList = async () => {
    const res: any = await getAppEventList(info.id);
    if (res.code === 200) setAppFlowList(res.data);
  };

  const getEventList = async () => {
    const res: any = await queryEventItemBySceneId({ sceneId: info.id });

    if (res.code === 200) {
      dispatch(updateEventTopicList(res.data
        .filter(v => !v.topic.includes('**empty**')) // 过滤掉topic包含**empty**的项目
        .map(v => {
          return { label: v.name, value: v.topic };
        })));
    }
  };


  useEffect(() => {
    getEventList();
    getAppFlowList();
  }, []);

  return (
    <FlowEditor initialData={appFlowList} useDefault={false} />
  );
};

export default ApplicationContainer;