import React, { useMemo } from 'react';

export default function LinkItem({ typeKey, linkConfig, history, ...data }) {
  const config = useMemo(() => linkConfig[typeKey] || {}, [typeKey, linkConfig]);
  const isVisisble = useMemo(() => {
    let flag = true;
    if (Array.isArray(config.deps) && config.deps.length > 0) {
      const flagArr = config.deps.map(itemArr => {
        let vData = data;
        const vArr = itemArr.map(v => vData[v]);
        return vArr.findIndex(v => !v) === -1;
      });
      if (config.condition === 'and') {
        flag = flagArr.findIndex(v => !v) === -1;
      } else {
        flag = flagArr.find(v => !!v) > -1;
      }
    } else {
      flag = false;
    }
    return flag;
  }, [config.condition, config.deps, data]);
  return !isVisisble ? <></> : <config.ItemComponent history={history} {...data} />;
}
