'use strict';

require('../../../../utils/index.cjs');
var log = require('../../../../utils/log.cjs');

function removeFromCheckedKeys(halfCheckedKeys, checkedKeys) {
  const filteredKeys = /* @__PURE__ */ new Set();
  halfCheckedKeys.forEach((key) => {
    if (!checkedKeys.has(key))
      filteredKeys.add(key);
  });
  return filteredKeys;
}
function isCheckDisabled(node) {
  const { disabled, disableCheckbox, checkable } = node || {};
  return !!(disabled || disableCheckbox) || checkable === false;
}
function fillConductCheck(keys, levelEntities, maxLevel, syntheticGetCheckDisabled) {
  const checkedKeys = new Set(keys);
  const halfCheckedKeys = /* @__PURE__ */ new Set();
  for (let level = 0; level <= maxLevel; level += 1) {
    const entities = levelEntities.get(level) || /* @__PURE__ */ new Set();
    entities.forEach((entity) => {
      const { key, node, children = [] } = entity;
      if (checkedKeys.has(key) && !syntheticGetCheckDisabled(node)) {
        children.filter((childEntity) => !syntheticGetCheckDisabled(childEntity.node)).forEach((childEntity) => {
          checkedKeys.add(childEntity.key);
        });
      }
    });
  }
  const visitedKeys = /* @__PURE__ */ new Set();
  for (let level = maxLevel; level >= 0; level -= 1) {
    const entities = levelEntities.get(level) || /* @__PURE__ */ new Set();
    entities.forEach((entity) => {
      const { parent, node } = entity;
      if (syntheticGetCheckDisabled(node) || !entity.parent || visitedKeys.has(entity.parent.key))
        return;
      if (syntheticGetCheckDisabled(entity.parent.node)) {
        visitedKeys.add(parent.key);
        return;
      }
      let allChecked = true;
      let partialChecked = false;
      (parent.children || []).filter((childEntity) => !syntheticGetCheckDisabled(childEntity.node)).forEach(({ key }) => {
        const checked = checkedKeys.has(key);
        if (allChecked && !checked)
          allChecked = false;
        if (!partialChecked && (checked || halfCheckedKeys.has(key)))
          partialChecked = true;
      });
      if (allChecked)
        checkedKeys.add(parent.key);
      if (partialChecked)
        halfCheckedKeys.add(parent.key);
      visitedKeys.add(parent.key);
    });
  }
  return {
    checkedKeys: Array.from(checkedKeys),
    halfCheckedKeys: Array.from(removeFromCheckedKeys(halfCheckedKeys, checkedKeys))
  };
}
function cleanConductCheck(keys, halfKeys, levelEntities, maxLevel, syntheticGetCheckDisabled) {
  const checkedKeys = new Set(keys);
  let halfCheckedKeys = new Set(halfKeys);
  for (let level = 0; level <= maxLevel; level += 1) {
    const entities = levelEntities.get(level) || /* @__PURE__ */ new Set();
    entities.forEach((entity) => {
      const { key, node, children = [] } = entity;
      if (!checkedKeys.has(key) && !halfCheckedKeys.has(key) && !syntheticGetCheckDisabled(node)) {
        children.filter((childEntity) => !syntheticGetCheckDisabled(childEntity.node)).forEach((childEntity) => {
          checkedKeys.delete(childEntity.key);
        });
      }
    });
  }
  halfCheckedKeys = /* @__PURE__ */ new Set();
  const visitedKeys = /* @__PURE__ */ new Set();
  for (let level = maxLevel; level >= 0; level -= 1) {
    const entities = levelEntities.get(level) || /* @__PURE__ */ new Set();
    entities.forEach((entity) => {
      const { parent, node } = entity;
      if (syntheticGetCheckDisabled(node) || !entity.parent || visitedKeys.has(entity.parent.key))
        return;
      if (syntheticGetCheckDisabled(entity.parent.node)) {
        visitedKeys.add(parent.key);
        return;
      }
      let allChecked = true;
      let partialChecked = false;
      (parent.children || []).filter((childEntity) => !syntheticGetCheckDisabled(childEntity.node)).forEach(({ key }) => {
        const checked = checkedKeys.has(key);
        if (allChecked && !checked)
          allChecked = false;
        if (!partialChecked && (checked || halfCheckedKeys.has(key)))
          partialChecked = true;
      });
      if (!allChecked)
        checkedKeys.delete(parent.key);
      if (partialChecked)
        halfCheckedKeys.add(parent.key);
      visitedKeys.add(parent.key);
    });
  }
  return {
    checkedKeys: Array.from(checkedKeys),
    halfCheckedKeys: Array.from(removeFromCheckedKeys(halfCheckedKeys, checkedKeys))
  };
}
function conductCheck(keyList, checked, keyEntities, maxLevel, levelEntities, getCheckDisabled) {
  const warningMissKeys = [];
  let syntheticGetCheckDisabled;
  if (getCheckDisabled)
    syntheticGetCheckDisabled = getCheckDisabled;
  else
    syntheticGetCheckDisabled = isCheckDisabled;
  const keys = new Set(
    keyList.filter((key) => {
      const hasEntity = !!keyEntities[key];
      if (!hasEntity)
        warningMissKeys.push(key);
      return hasEntity;
    })
  );
  log.note(
    !warningMissKeys.length,
    `Tree missing follow keys: ${warningMissKeys.slice(0, 100).map((key) => `'${key}'`).join(", ")}`
  );
  let result;
  if (checked === true) {
    result = fillConductCheck(
      keys,
      levelEntities,
      maxLevel,
      syntheticGetCheckDisabled
    );
  } else {
    result = cleanConductCheck(
      keys,
      checked.halfCheckedKeys,
      levelEntities,
      maxLevel,
      syntheticGetCheckDisabled
    );
  }
  return result;
}

exports.conductCheck = conductCheck;
exports.isCheckDisabled = isCheckDisabled;
//# sourceMappingURL=conductUtil.cjs.map
