import { ref, computed, isRef } from 'vue';
import { useRectSelect } from '../useRectSelect/index.mjs';

const useCheckAll = (list, options) => {
  const checkedIndexs = ref(/* @__PURE__ */ new Set());
  const checkedIndexsArr = computed(() => [...checkedIndexs.value]);
  const handleList = computed(() => Array.from(isRef(list) ? list.value : list));
  const canSelectList = computed(() => {
    const disabledKey = options?.disabledKey;
    if (disabledKey) {
      return handleList.value.filter((it) => !it[disabledKey]);
    }
    return handleList.value;
  });
  const checkAll = computed(() => {
    return canSelectList.value.length === checkedIndexs.value.size;
  });
  const isIndeterminate = computed(() => !checkAll.value && checkedIndexs.value.size > 0);
  const handleCheckAllChange = (val) => {
    if (!val) {
      checkedIndexs.value = /* @__PURE__ */ new Set();
    } else {
      checkedIndexs.value = new Set(
        handleList.value.reduce((acc, cur, idx) => {
          const disabledKey = options?.disabledKey;
          if (disabledKey && cur[disabledKey]) {
            return acc;
          }
          if (options?.key) {
            return [...acc, cur[options.key]];
          }
          return [...acc, idx];
        }, [])
      );
    }
  };
  const toggleSelect = (index) => {
    const disabledKey = options?.disabledKey;
    if (disabledKey) {
      const key = options?.key;
      if (key) {
        const findItem = handleList.value.find((it) => it[key] === index);
        if (findItem?.[disabledKey]) {
          return;
        }
      } else if (handleList.value[index][disabledKey]) {
        return;
      }
    }
    if (checkedIndexs.value.has(index)) {
      checkedIndexs.value.delete(index);
    } else {
      checkedIndexs.value.add(index);
    }
  };
  const { mousedown, mousemove: baseMousemove, mouseup, isElementInSelection } = useRectSelect(options?.rectSelectParams?.options);
  const mousemove = (e) => {
    if (!options?.rectSelectParams?.canSelectListRefs?.value) {
      return;
    }
    const isMove = baseMousemove(e);
    if (!isMove) {
      return;
    }
    options?.rectSelectParams?.canSelectListRefs?.value.forEach((item, index) => {
      if (options?.disabledKey && handleList.value[index][options.disabledKey]) {
        return;
      }
      if (item && isElementInSelection(item)) {
        const key = options?.key ? handleList.value[index][options.key] : index;
        if (e.ctrlKey) {
          checkedIndexs.value.delete(key);
        } else {
          checkedIndexs.value.add(key);
        }
      }
    });
  };
  return {
    /**选中了哪些index（或key）的图片 Set */
    checkedIndexs,
    /**选中了哪些index（或key）的图片 Array */
    checkedIndexsArr,
    /**是否全选 */
    checkAll,
    /**是否半选 */
    isIndeterminate,
    /**点击全选 */
    handleCheckAllChange,
    /**切换选中 */
    toggleSelect,
    /**
     * useRectSelect 响应的值
     */
    mousedown,
    mousemove,
    mouseup,
    /**当前元素是否在选择框中 */
    isElementInSelection
  };
};

export { useCheckAll };
//# sourceMappingURL=index.mjs.map
