/**
 * 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/dep-60299c2f.js';
import useVModel from '../../../hooks/use-vmodel.js';
import useDefaultValue from '../../../hooks/use-default-value.js';
import { isValueInvalid, isEmptyValues, getCascaderValue, getTreeValue } from '../utils/helper.js';
import { treeNodesEffect, treeStoreExpendEffect } from '../utils/effect.js';
import '../../../_chunks/dep-eb467ddf.js';
import '../../../_chunks/dep-83afdf41.js';
import '../../../_chunks/dep-48540ab4.js';
import '../../../_chunks/dep-22504add.js';
import '../../../_chunks/dep-24cf4e98.js';
import '../../../_chunks/dep-5210251b.js';
import '../../../_chunks/dep-fe18d4b0.js';
import '../../../_chunks/dep-783df41c.js';
import '../../../_chunks/dep-b31eb7d9.js';
import '../../../_chunks/dep-70c789e5.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
