/**
 * ldesign v0.2.14
 * (c) 2022/11/2 16:16 built by swimly
 * @license MIT
 */

import { reactive, computed, toRefs, watch, nextTick } from '@vue/composition-api';
import isEqual from 'lodash/isEqual';
import isFunction from 'lodash/isFunction';
import { T as TreeStore } from '../../../chunks/tree-store.js';
import useVModel from '../../../hooks/use-vmodel.js';
import useDefaultValue from '../../../hooks/use-default-value.js';
import { getTreeValue, isValueInvalid, isEmptyValues, getCascaderValue } from '../utils/helper.js';
import { treeNodesEffect, treeStoreExpendEffect } from '../utils/effect.js';
import '../../../chunks/defineProperty-838e4192.js';
import '../../../chunks/get-5e1abd10.js';
import '../../../chunks/isArray-cd7357b9.js';
import '../../../chunks/isSymbol-a38a5184.js';
import '../../../chunks/isObjectLike-c22c7e3b.js';
import '../../../chunks/_commonjsHelpers-2e6815bd.js';
import '../../../chunks/isFunction-91acade7.js';
import '../../../chunks/tree-node.js';
import '../../../chunks/_isIndex-52887d1e.js';
import '../../../chunks/log.js';
import 'lodash/kebabCase';
import 'lodash/isEmpty';
import 'lodash/isNumber';
import 'lodash/cloneDeep';

const useContext = (props, setInnerValue, innerPopupVisible, setPopupVisible) => {
  const statusContext = reactive({
    treeStore: null,
    inputVal: null,
    scopeVal: void 0,
    treeNodes: [],
    expend: []
  });
  return {
    statusContext,
    cascaderContext: computed(() => {
      const {
        size,
        checkStrictly,
        lazy,
        multiple,
        filterable,
        clearable,
        checkProps,
        max,
        disabled,
        showAllLevels,
        minCollapsedNum,
        valueType
      } = props;
      return {
        value: statusContext.scopeVal,
        size,
        checkStrictly,
        lazy,
        multiple,
        filterable,
        clearable,
        checkProps,
        max,
        disabled,
        showAllLevels,
        minCollapsedNum,
        valueType,
        visible: innerPopupVisible.value,
        ...statusContext,
        setTreeNodes: nodes => {
          statusContext.treeNodes = nodes;
        },
        setValue: (val, source, node) => {
          if (isEqual(val, statusContext.scopeVal)) return;
          setInnerValue(val, {
            source,
            node
          });
        },
        setVisible: setPopupVisible,
        setInputVal: val => {
          statusContext.inputVal = val;
        },
        setExpend: val => {
          statusContext.expend = val;
        }
      };
    })
  };
};
const useCascaderContext = props => {
  const {
    disabled
  } = props;
  const {
    value,
    popupVisible
  } = toRefs(props);
  const [innerValue, setInnerValue] = useVModel(value, props.defaultValue, props.onChange, "change");
  const [innerPopupVisible, setPopupVisible] = useDefaultValue(popupVisible, false, props.onPopupVisibleChange, "popupVisible", "popup-visible-change");
  const {
    cascaderContext,
    statusContext
  } = useContext(props, setInnerValue, innerPopupVisible, setPopupVisible);
  const isFilterable = computed(() => Boolean(props.filterable || isFunction(props.filter)));
  const updatedTreeNodes = () => {
    const {
      inputVal,
      treeStore,
      setTreeNodes
    } = cascaderContext.value;
    treeNodesEffect(inputVal, treeStore, setTreeNodes, props.filter);
  };
  const updateExpend = () => {
    const {
      value: value2,
      treeStore
    } = cascaderContext.value;
    const {
      expend
    } = statusContext;
    treeStoreExpendEffect(treeStore, value2, expend);
    treeStore.replaceChecked(getTreeValue(value2));
  };
  watch(() => props.options, () => {
    const {
      options,
      keys = {},
      checkStrictly,
      lazy,
      load,
      valueMode
    } = props;
    const {
      treeStore
    } = statusContext;
    if (!options.length && !treeStore) return;
    if (!treeStore) {
      const store = new TreeStore({
        keys: {
          ...keys,
          children: typeof keys.children === "string" ? keys.children : "children"
        },
        checkable: true,
        expandMutex: true,
        expandParent: true,
        checkStrictly,
        lazy,
        load,
        valueMode,
        onLoad: () => {
          nextTick(() => {
            store.refreshNodes();
            updatedTreeNodes();
          });
        }
      });
      store.append(options);
      statusContext.treeStore = store;
    } else {
      treeStore.reload(options);
      treeStore.refreshNodes();
    }
    updateExpend();
    updatedTreeNodes();
  }, {
    immediate: true
  });
  watch(() => {
    const {
      checkStrictly,
      lazy,
      load,
      valueMode
    } = props;
    return JSON.stringify({
      valueMode,
      checkStrictly,
      lazy,
      load
    });
  }, () => {
    const {
      treeStore
    } = statusContext;
    if (!treeStore) return;
    const {
      checkStrictly,
      lazy,
      load,
      valueMode
    } = props;
    const treeProps = {
      checkStrictly,
      disabled,
      load,
      lazy,
      valueMode
    };
    treeStore.setConfig(treeProps);
  }, {
    immediate: true
  });
  watch(innerValue, () => {
    const {
      setValue,
      multiple,
      valueType
    } = cascaderContext.value;
    if (isValueInvalid(innerValue.value, cascaderContext.value)) setValue(multiple ? [] : "", "invalid-value");
    if (!isEmptyValues(innerValue)) statusContext.scopeVal = getCascaderValue(innerValue.value, valueType, multiple);else statusContext.scopeVal = multiple ? [] : "";
    if (!statusContext.treeStore) return;
    updateExpend();
    updatedTreeNodes();
  }, {
    immediate: true
  });
  watch(() => innerPopupVisible.value && isFilterable.value, visible => {
    const {
      setInputVal
    } = cascaderContext.value;
    if (visible) setInputVal("");
  });
  watch(() => statusContext.inputVal, () => {
    updatedTreeNodes();
  });
  return {
    cascaderContext,
    isFilterable
  };
};

export { useCascaderContext, useContext };
//# sourceMappingURL=index.js.map
