"use strict";

var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");

exports.__esModule = true;
exports.default = void 0;

var _regenerator = _interopRequireDefault(require("@babel/runtime/regenerator"));

var _asyncToGenerator2 = _interopRequireDefault(require("@babel/runtime/helpers/asyncToGenerator"));

var _common = require("./common");

var APPKEY = 10205;

var setChildrenData = function setChildrenData(data, item, idMap, key, rootChecked) {
  if (rootChecked === void 0) {
    rootChecked = false;
  }

  var cloneData = (0, _common.cloneDeep)(data);
  var childrenData = cloneData.map(function (v, index) {
    var splitName = v.districtName.split('-');
    v.simpleName = splitName[splitName.length - 1];
    v.isChecked = false;
    v.index = index + 1;
    v.isIndeterminate = false;
    v._erpZoom = key;
    v._parent = item;
    v.childrenNode = [];
    idMap.set(v.districtID, v);
    return v;
  });
  var childrenList = [{
    simpleName: '不限',
    index: 0,
    isChecked: rootChecked,
    isChoose: false,
    isIndeterminate: false,
    districtLevel: item.districtLevel,
    _erpZoom: key,
    _parent: item
  }].concat(childrenData);
  item.childrenNode = childrenList;
  return childrenList;
};

var _default = {
  methods: {
    /**
     * 获取子节点数据
     * @param: item key
     * **/
    getAreaChildren: function getAreaChildren(idMap, item, key, rootChecked) {
      var _this = this;

      return (0, _asyncToGenerator2.default)( /*#__PURE__*/_regenerator.default.mark(function _callee() {
        var method, param, data;
        return _regenerator.default.wrap(function _callee$(_context) {
          while (1) {
            switch (_context.prev = _context.next) {
              case 0:
                _context.prev = 0;
                method = 'address.addressBgTerritory.getMultiLevelThinkList';
                param = {
                  method: method,
                  jsonrpc: '2.0',
                  params: {
                    districtID: item.districtID,
                    showParam: 0
                  }
                };
                _context.next = 5;
                return _this.$http(method, APPKEY, param);

              case 5:
                data = _context.sent;

                if (!(data && data.districtList)) {
                  _context.next = 8;
                  break;
                }

                return _context.abrupt("return", setChildrenData(data.districtList, item, idMap, key, rootChecked));

              case 8:
                _context.next = 12;
                break;

              case 10:
                _context.prev = 10;
                _context.t0 = _context["catch"](0);

              case 12:
              case "end":
                return _context.stop();
            }
          }
        }, _callee, null, [[0, 10]]);
      }))();
    },
    getAreaLinkData: function getAreaLinkData(idArry, idMap, areaTree) {
      var _this2 = this;

      return (0, _asyncToGenerator2.default)( /*#__PURE__*/_regenerator.default.mark(function _callee2() {
        var data, treeData, newMap, loopSetMap, loopAreaSet;
        return _regenerator.default.wrap(function _callee2$(_context2) {
          while (1) {
            switch (_context2.prev = _context2.next) {
              case 0:
                _context2.prev = 0;

                if (!(!idArry || !idArry.length)) {
                  _context2.next = 3;
                  break;
                }

                return _context2.abrupt("return");

              case 3:
                _context2.next = 5;
                return _this2.$http('address.regionInfo.getInfoByDistrictId', '50243', {
                  districtIds: idArry
                });

              case 5:
                data = _context2.sent;

                if (data) {
                  // 大区对象
                  treeData = data.childrenNode;
                  newMap = new Map();

                  loopSetMap = function loopSetMap(mapData) {
                    newMap.set(mapData.districtID, mapData);
                    mapData.childrenNode && mapData.childrenNode.map(function (item) {
                      loopSetMap(item);
                    });
                  };

                  treeData.map(function (item) {
                    loopSetMap(item);
                  });

                  loopAreaSet = function loopAreaSet(areaItem) {
                    if (areaItem.districtID) {
                      if (!areaItem.childrenNode || !areaItem.childrenNode.length) {
                        // 排除不限
                        var childrenData = newMap.get(areaItem.districtID);

                        if (!childrenData || !childrenData.childrenNode) {
                          return;
                        }

                        setChildrenData(childrenData.childrenNode, areaItem, idMap, areaItem._erpZoom + 1);
                      }

                      areaItem.childrenNode.map(function (item) {
                        loopAreaSet(item);
                      });
                    }
                  };

                  areaTree.childrenNode.map(function (areaItem) {
                    loopAreaSet(areaItem);
                  });
                }

                _context2.next = 11;
                break;

              case 9:
                _context2.prev = 9;
                _context2.t0 = _context2["catch"](0);

              case 11:
              case "end":
                return _context2.stop();
            }
          }
        }, _callee2, null, [[0, 9]]);
      }))();
    },

    /**
     * 向上(父级)递归数据处理
     * @param: item
     * **/
    setFather: function setFather(item) {
      if (item._parent && item._erpZoom !== 0) {
        var bortherItem = item._parent.childrenNode.slice(1, item._parent.childrenNode.length);

        var allChecked = bortherItem.every(function (item) {
          return item.isChecked;
        });

        var allNoChecked = item._parent.childrenNode.every(function (item) {
          return item.isChecked === false;
        });

        var someIndeterminate = item._parent.childrenNode.some(function (item) {
          return item.isIndeterminate === true;
        });

        var firstItem = item._parent.childrenNode[0].isChecked;
        item._parent._parent && item._parent._parent.childrenNode.map(function (item) {
          item.isChoose = false;
        });
        item._parent.isChoose = true;
        item._parent.isChecked = firstItem || allChecked;
        item._parent.isIndeterminate = firstItem ? false : someIndeterminate || !(allNoChecked || allChecked);

        if (item._parent._parent && item._erpZoom !== 1) {
          item._parent._parent.childrenNode[0].isChecked = false;
        }

        this.setFather(item._parent);
      }
    },

    /**
     * 同级数据处理
     * @param: item itemIndex type
     * **/
    setPeer: function setPeer(item, itemIndex, type) {
      var _this3 = this;

      if (itemIndex === 0) {
        item._parent && item._parent.childrenNode.map(function (v, vIndex) {
          if (v.isChoose || v.isChecked || v.isIndeterminate) {
            _this3.clearChildren(v);
          }

          if (vIndex === 0) {
            v.isChecked = type;
          } else {
            v.isChecked = false;
            v.isIndeterminate = false;
          }

          v.isChoose = false;
        });
      } else {
        if (item._erpZoom !== 0) {
          type && item._parent && (item._parent.childrenNode[0].isChecked = false);
        } else {
          item._parent && (item._parent.isChecked = false);
        }

        item._parent && item._parent.childrenNode.map(function (v) {
          v.isChoose = false;
        });
        item.isChoose = type;
      }

      item.isChecked = type;
      item.isIndeterminate = false;
    },

    /**
     * 向下递归数据处理
     * @param: item
     * **/
    clearChildren: function clearChildren(item) {
      var _this4 = this;

      item.childrenNode && item.childrenNode.map(function (v) {
        if (v.isChoose || v.isChecked || v.isIndeterminate) {
          _this4.clearChildren(v);

          v.isChoose = false;
          v.isChecked = false;
          v.isIndeterminate = false;
        }
      });
    },

    /**
     * 清除子节点的choose checked Indeterminate状态
     * @param {*} data
     */
    clearStatus: function clearStatus(data) {
      var clearLoop = function clearLoop(item) {
        item.childrenNode && item.childrenNode.map(function (v) {
          if (v.isChoose || v.isChecked || v.isIndeterminate) {
            clearLoop(v);
          }
        });
        item.isChoose = false;
        item.isChecked = false;
        item.isIndeterminate = false;
      };

      clearLoop(data);
    },

    /**
     * 打开当前节点的相关链路
     * @param {*} item
     * @param {*} selectData 链路二维数组
     */
    openAddressLink: function openAddressLink(item, selectData) {
      var openFatherLink = function openFatherLink(currentItem) {
        var _parent = currentItem._parent;

        if (currentItem && currentItem._erpZoom !== 0) {
          _parent.childrenNode && (selectData[currentItem._erpZoom] = _parent.childrenNode);
          openFatherLink(_parent);
        }
      };

      openFatherLink(item);
    }
  }
};
exports.default = _default;