import { getOrganList } from 'actions/base.action';
import { useEffect, useState } from 'react';
import { isEmpty, concat, compact } from 'lodash';
export default function useOrgan(params = {}, isEdit) {
    const [treeData, setTreeData] = useState([]);
    const [organIds, setOrganIds] = useState([]);
    useEffect(() => {
        if (!isEdit) {
            getOrganList(Object.assign({}, params)).then(setTreeData);
        }
        if (isEdit && isEmpty(organIds)) {
            getOrganList(Object.assign({}, params)).then(setTreeData);
        }
    }, []);
    useEffect(() => {
        if (isEdit && !isEmpty(organIds)) {
            Promise.all(['', ...organIds].map((item) => getOrganList({
                organId: item,
            }))).then((values) => {
                setTreeData(concat(...compact(values)));
            });
        }
    }, [organIds]);
    const onLoadData = ({ id, value }) => {
        return getOrganList({
            organId: value,
        }).then((data) => {
            setTreeData(treeData.concat(data));
        });
    };
    return [
        treeData,
        {
            onLoadData,
            setOrganIds,
            organExpandedKeys: organIds,
        },
    ];
}