/* eslint-disable prefer-destructuring,guard-for-in */
import { fetchDomains, switchDomain, currentDomain } from '../services/domain';
import { getDomain, setDomain } from '../utils/auth';

const findTarget = (list, dmpath) => {
  // 循环 list，元素el
  // 如果 el.value == dmpath, return el;
  // 否则 findTarget（el.children, dmpath）
  // 如果找到，返回找到的
  // 如果没找到，继续循环
  // 循环结束： return null；
  for (let i = 0; i < list.length; i++) {
    if (list[i].value === dmpath) {
      return list[i];
    } else {
      const data = findTarget(list[i].children, dmpath);
      // console.log(data);
      if (data) {
        return data;
      }
    }
  }
  return null;
};
const createData = async (list, dmpath, payload) => {
  console.log(list);
  console.log(dmpath);
  // 根据path用fetchDomains 查出children
  let childrens;
  let pathArray;
  if (dmpath !== '/') {
    childrens = await fetchDomains(dmpath);
    console.log(childrens);
    const childrenPath = childrens[0].path;
    pathArray = [...payload, childrenPath];
  } else {
    pathArray = [...payload];
  }
  const childrenData = [];
  childrens.map(({ name, path }) => {
    childrenData.push({ label: name, value: path, children: [] });
    return childrenData;
  });
  // 根据path在list查出需要添加children的对象target
  const findData = findTarget(list, dmpath);
  findData.children = childrenData;
  console.log(findData);
  const data = list;
  console.log(list);
  return { data, pathArray };
};
export default {

  namespace: 'domain',

  state: {
    init: undefined,
    list: [],
    listChildren: [],
    arrayPath: [],
    cols: '',
  },

  subscriptions: {
    setup({ dispatch }) {  // eslint-disable-line
    },
  },

  effects: {
    *fetch(ignored, { put, call }) {
      const domain = yield call(getDomain);
      const init = domain ? domain.path : undefined;
      const list = yield call(fetchDomains, '/', true);
      const datas = [];
      list.map(({ name, path }) => {
        datas.push({ label: name, value: path, children: [] });
        return datas;
      });
      yield put({ type: 'queryInit', payload: init });
      yield put({ type: 'queryArryPath', payload: ['/'] });
      yield put({ type: 'queryList', payload: datas });
    },

    *fetchParam({ payload }, { put, call, select }) {
      console.log(payload);
      let dmpath = '';
      if (payload.length === 1) {
        dmpath = payload[0];
      } else if (payload.length === 2) {
        dmpath = payload[1];
      } else {
        dmpath = payload[2];
      }
      const { list } = yield select(state => state.domain);
      // const target = yield call(findTarget, list, dmpath);
      // console.log(target);
      const datas = yield call(createData, list, dmpath, payload);
      console.log(datas);
      yield put({ type: 'queryList', payload: datas.data });
      yield put({ type: 'queryArryPath', payload: datas.pathArray });
    },
    *switch({ payload: domainPath }, { put, call }) {
      yield call(switchDomain, domainPath);
      const { path, name } = yield call(currentDomain);
      yield call(setDomain, name, path);
      yield put({ type: 'queryInit', payload: path });
    },
  },

  reducers: {
    queryInit(state, { payload: init }) {
      return {
        ...state,
        init,
      };
    },
    queryList(state, { payload: list }) {
      return {
        ...state,
        list,
      };
    },
    queryListChildren(state, { payload: listChildren }) {
      return {
        ...state,
        listChildren,
      };
    },
    queryArryPath(state, { payload: arrayPath }) {
      return {
        ...state,
        arrayPath,
      };
    },
  },

};
