/**
 * Created by pzf on 18/9/1.
 *
 */
import lang from 'lodash/lang';
/** *******************Select************************** */

export function findFeeTypeByCode(categories, code) {
  // 用for可以return
  var category;
  var child;

  for (var i = 0; i < categories.length; i += 1) {
    category = categories[i];

    for (var j = 0; j < category.children.length; j += 1) {
      child = category.children[j];

      if (child.feeTypeCode === code) {
        return child;
      }
    }
  }
}
/** *******************Panel************************** */

function buildChoosedItem(item) {
  return {
    icon: item.iconCode,
    code: item.feeTypeCode,
    name: item.feeTypeName
  };
}
/**
 * 清空选择
 * @param choosed
 */


function cleanShoosed(choosed) {
  choosed.forEach(function (item) {
    delete item.selected;
  });
  choosed.length = 0;
}
/**
 * 补全hot中的其他信息
 * @param hots
 * @param categories
 * @returns {*}
 */


export function getFullHots(hots, categories) {
  var fullHots = [];
  categories.forEach(function (ca) {
    ca.children.forEach(function (c) {
      if (hots.indexOf(c.feeTypeCode) >= 0) {
        fullHots.push(c);
      }
    });
  });
  return fullHots;
}
export function buildOnSelectData(choosed) {
  return choosed;
}
export function itemSelected(item) {
  item.selected = true;
}
export function itemDeSelected(item) {
  delete item.selected;
}
/**
 * 是否已选
 * @param choosed
 * @param item
 * @returns {number | * | BigInt | T}
 */

export function isChoosed(choosed, item) {
  return choosed.find(function (ch) {
    return item.feeTypeCode === ch.code;
  });
}
/**
 * 是否选中
 * @param selected
 * @param item
 * @returns {number | * | BigInt | T}
 */

export function isSelected(selected, item) {
  return selected.find(function (ch) {
    return item.feeTypeCode === ch;
  });
}
/**
 * 移除,单个的情况
 * @param choosed
 * @param feeTypeCode 内外调用存在差异化，所以直接将obj换成code
 */

export function removeChoosed(categories, choosed, feeTypeCode) {
  var finded = choosed.find(function (ch) {
    return feeTypeCode === ch.code;
  });
  var index = choosed.indexOf(finded);

  if (index >= 0) {
    choosed.splice(index, 1);
  }

  var feeType = findFeeTypeByCode(categories, feeTypeCode);
  itemDeSelected(feeType);
}
/**
 * 移除,单个或多个，外面调用
 * @param categories
 * @param choosed
 * @param items
 */

export function removeChoosedOuter(categories, choosed, items) {
  if (lang.isArray(items)) {
    items.forEach(function (it) {
      removeChoosed(categories, choosed, it.code);
    });
  } else {
    removeChoosed(categories, choosed, items.code);
  }
}
export function addToChoosed(choosed, item, multiple, categories) {
  if (multiple) {
    choosed.push(buildChoosedItem(item));
    itemSelected(item);
  } else {
    choosed.forEach(function (ch) {
      var feeType = findFeeTypeByCode(categories, ch.code);
      itemDeSelected(feeType);
    });
    cleanShoosed(choosed);
    choosed.push(buildChoosedItem(item));
    itemSelected(item);
  }
}
export function getSearchResult(categories, keywords) {
  var searchResult = [];
  categories.forEach(function (category) {
    category.children.forEach(function (item) {
      if (item.feeTypeName.indexOf(keywords) >= 0) {
        searchResult.push(item);
      }
    });
  });
  return searchResult;
}
//# sourceMappingURL=util.js.map
