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

'use strict';

Object.defineProperty(exports, '__esModule', { value: true });

var VueCompositionAPI = require('@vue/composition-api');
var isEqual = require('lodash/isEqual');
var isFunction = require('lodash/isFunction');
var treeStore = require('../../../chunks/tree-store.js');
var hooks_useVmodel = require('../../../hooks/use-vmodel.js');
var hooks_useDefaultValue = require('../../../hooks/use-default-value.js');
var components_cascader_utils_helper = require('../utils/helper.js');
var components_cascader_utils_effect = require('../utils/effect.js');
require('../../../chunks/defineProperty-838e4192.js');
require('../../../chunks/get-5e1abd10.js');
require('../../../chunks/isArray-cd7357b9.js');
require('../../../chunks/isSymbol-a38a5184.js');
require('../../../chunks/isObjectLike-c22c7e3b.js');
require('../../../chunks/_commonjsHelpers-2e6815bd.js');
require('../../../chunks/isFunction-91acade7.js');
require('../../../chunks/tree-node.js');
require('../../../chunks/_isIndex-52887d1e.js');
require('../../../chunks/log.js');
require('lodash/kebabCase');
require('lodash/isEmpty');
require('lodash/isNumber');
require('lodash/cloneDeep');

function _interopDefaultLegacy (e) { return e && typeof e === 'object' && 'default' in e ? e : { 'default': e }; }

var isEqual__default = /*#__PURE__*/_interopDefaultLegacy(isEqual);
var isFunction__default = /*#__PURE__*/_interopDefaultLegacy(isFunction);

const useContext = (props, setInnerValue, innerPopupVisible, setPopupVisible) => {
  const statusContext = VueCompositionAPI.reactive({
    treeStore: null,
    inputVal: null,
    scopeVal: void 0,
    treeNodes: [],
    expend: []
  });
  return {
    statusContext,
    cascaderContext: VueCompositionAPI.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__default["default"](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
  } = VueCompositionAPI.toRefs(props);
  const [innerValue, setInnerValue] = hooks_useVmodel["default"](value, props.defaultValue, props.onChange, "change");
  const [innerPopupVisible, setPopupVisible] = hooks_useDefaultValue["default"](popupVisible, false, props.onPopupVisibleChange, "popupVisible", "popup-visible-change");
  const {
    cascaderContext,
    statusContext
  } = useContext(props, setInnerValue, innerPopupVisible, setPopupVisible);
  const isFilterable = VueCompositionAPI.computed(() => Boolean(props.filterable || isFunction__default["default"](props.filter)));
  const updatedTreeNodes = () => {
    const {
      inputVal,
      treeStore,
      setTreeNodes
    } = cascaderContext.value;
    components_cascader_utils_effect.treeNodesEffect(inputVal, treeStore, setTreeNodes, props.filter);
  };
  const updateExpend = () => {
    const {
      value: value2,
      treeStore
    } = cascaderContext.value;
    const {
      expend
    } = statusContext;
    components_cascader_utils_effect.treeStoreExpendEffect(treeStore, value2, expend);
    treeStore.replaceChecked(components_cascader_utils_helper.getTreeValue(value2));
  };
  VueCompositionAPI.watch(() => props.options, () => {
    const {
      options,
      keys = {},
      checkStrictly,
      lazy,
      load,
      valueMode
    } = props;
    const {
      treeStore: treeStore$1
    } = statusContext;
    if (!options.length && !treeStore$1) return;
    if (!treeStore$1) {
      const store = new treeStore.TreeStore({
        keys: {
          ...keys,
          children: typeof keys.children === "string" ? keys.children : "children"
        },
        checkable: true,
        expandMutex: true,
        expandParent: true,
        checkStrictly,
        lazy,
        load,
        valueMode,
        onLoad: () => {
          VueCompositionAPI.nextTick(() => {
            store.refreshNodes();
            updatedTreeNodes();
          });
        }
      });
      store.append(options);
      statusContext.treeStore = store;
    } else {
      treeStore$1.reload(options);
      treeStore$1.refreshNodes();
    }
    updateExpend();
    updatedTreeNodes();
  }, {
    immediate: true
  });
  VueCompositionAPI.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
  });
  VueCompositionAPI.watch(innerValue, () => {
    const {
      setValue,
      multiple,
      valueType
    } = cascaderContext.value;
    if (components_cascader_utils_helper.isValueInvalid(innerValue.value, cascaderContext.value)) setValue(multiple ? [] : "", "invalid-value");
    if (!components_cascader_utils_helper.isEmptyValues(innerValue)) statusContext.scopeVal = components_cascader_utils_helper.getCascaderValue(innerValue.value, valueType, multiple);else statusContext.scopeVal = multiple ? [] : "";
    if (!statusContext.treeStore) return;
    updateExpend();
    updatedTreeNodes();
  }, {
    immediate: true
  });
  VueCompositionAPI.watch(() => innerPopupVisible.value && isFilterable.value, visible => {
    const {
      setInputVal
    } = cascaderContext.value;
    if (visible) setInputVal("");
  });
  VueCompositionAPI.watch(() => statusContext.inputVal, () => {
    updatedTreeNodes();
  });
  return {
    cascaderContext,
    isFilterable
  };
};

exports.useCascaderContext = useCascaderContext;
exports.useContext = useContext;
//# sourceMappingURL=index.js.map
