/**
 * ldesign v0.3.13
 * (c) 2023 ldesign
 * @license MIT
 */

import { _ as _slicedToArray } from '../_chunks/dep-84acf3c2.mjs';
import { _ as _defineProperty } from '../_chunks/dep-2db7b3ec.mjs';
import { reactive, computed, toRefs, watch, nextTick } from 'vue';
import '../_chunks/dep-eba0ea6e.mjs';
import { _ as _baseIsEqual } from '../_chunks/dep-9c7945a2.mjs';
import { i as isFunction_1 } from '../_chunks/dep-5717e263.mjs';
import { i as isString_1 } from '../_chunks/dep-876fb3ec.mjs';
import { TreeStore } from '../_common/js/tree/tree-store.mjs';
import { useFormDisabled } from '../form/hooks.mjs';
import useVModel from '../hooks/useVModel.mjs';
import useDefaultValue from '../hooks/useDefaultValue.mjs';
import { getTreeValue, isValueInvalid, isEmptyValues, getCascaderValue } from './core/helper.mjs';
import { treeNodesEffect, treeStoreExpendEffect } from './core/effect.mjs';
import '../_chunks/dep-b8d7992c.mjs';
import '../_chunks/dep-255303ba.mjs';
import '../_chunks/dep-9cf86cf1.mjs';
import '../_chunks/dep-bc1d737a.mjs';
import '../_chunks/dep-4d7a675b.mjs';
import '../_chunks/dep-2e7f59b1.mjs';
import '../_chunks/dep-469b4ae0.mjs';
import '../_chunks/dep-a8bcacea.mjs';
import '../_chunks/dep-855db8bc.mjs';
import '../_chunks/dep-a87f3e4c.mjs';
import '../_chunks/dep-1e566bb5.mjs';
import '../_chunks/dep-1434aa15.mjs';
import '../_chunks/dep-3da99438.mjs';
import '../_chunks/dep-b57180d0.mjs';
import '../_chunks/dep-c8d7b6b4.mjs';
import '../_chunks/dep-e5eb9f23.mjs';
import '../_chunks/dep-9fe2ef12.mjs';
import '../_chunks/dep-672e7d1a.mjs';
import '../_chunks/dep-42a19209.mjs';
import '../_chunks/dep-9262ee2f.mjs';
import '../_chunks/dep-c42916c7.mjs';
import '../_chunks/dep-c989dff3.mjs';
import '../_chunks/dep-334a4f6b.mjs';
import '../_chunks/dep-8d3e9aa3.mjs';
import '../_chunks/dep-ccbc47f4.mjs';
import '../_chunks/dep-3f21269a.mjs';
import '../_chunks/dep-42cc3015.mjs';
import '../_chunks/dep-2bc37d52.mjs';
import '../_chunks/dep-6884627f.mjs';
import '../_chunks/dep-0fc02184.mjs';
import '../_chunks/dep-dcc83161.mjs';
import '../_chunks/dep-406cd044.mjs';
import '../_chunks/dep-327559e8.mjs';
import '../_chunks/dep-b2235f26.mjs';
import '../_chunks/dep-8b989474.mjs';
import '../_chunks/dep-1372b2d3.mjs';
import '../_chunks/dep-f1994936.mjs';
import 'mitt';
import '../_common/js/tree/tree-node.mjs';
import '../_chunks/dep-38ee6a8f.mjs';
import '../_chunks/dep-bf1e33c8.mjs';
import '../_chunks/dep-ed5ebb35.mjs';
import '../_chunks/dep-01e3f446.mjs';
import '../_chunks/dep-c81c0ee4.mjs';
import '../_chunks/dep-0d3dd4b5.mjs';
import '../_common/js/tree/tree-node-model.mjs';
import '../_chunks/dep-78847b2e.mjs';
import '../_chunks/dep-35e3b1aa.mjs';
import '../_chunks/dep-8932db97.mjs';
import '../_chunks/dep-550c24c1.mjs';
import '../_chunks/dep-350764bc.mjs';
import '../_common/js/log/log.mjs';
import '../_common/js/log/index.mjs';
import '../_chunks/dep-9a85dac1.mjs';
import '../_chunks/dep-888e80c4.mjs';
import '../_chunks/dep-095107f7.mjs';

var baseIsEqual = _baseIsEqual;

/**
 * Performs a deep comparison between two values to determine if they are
 * equivalent.
 *
 * **Note:** This method supports comparing arrays, array buffers, booleans,
 * date objects, error objects, maps, numbers, `Object` objects, regexes,
 * sets, strings, symbols, and typed arrays. `Object` objects are compared
 * by their own, not inherited, enumerable properties. Functions and DOM
 * nodes are compared by strict equality, i.e. `===`.
 *
 * @static
 * @memberOf _
 * @since 0.1.0
 * @category Lang
 * @param {*} value The value to compare.
 * @param {*} other The other value to compare.
 * @returns {boolean} Returns `true` if the values are equivalent, else `false`.
 * @example
 *
 * var object = { 'a': 1 };
 * var other = { 'a': 1 };
 *
 * _.isEqual(object, other);
 * // => true
 *
 * object === other;
 * // => false
 */
function isEqual(value, other) {
  return baseIsEqual(value, other);
}
var isEqual_1 = isEqual;

function ownKeys(object, enumerableOnly) { var keys = Object.keys(object); if (Object.getOwnPropertySymbols) { var symbols = Object.getOwnPropertySymbols(object); enumerableOnly && (symbols = symbols.filter(function (sym) { return Object.getOwnPropertyDescriptor(object, sym).enumerable; })), keys.push.apply(keys, symbols); } return keys; }
function _objectSpread(target) { for (var i = 1; i < arguments.length; i++) { var source = null != arguments[i] ? arguments[i] : {}; i % 2 ? ownKeys(Object(source), !0).forEach(function (key) { _defineProperty(target, key, source[key]); }) : Object.getOwnPropertyDescriptors ? Object.defineProperties(target, Object.getOwnPropertyDescriptors(source)) : ownKeys(Object(source)).forEach(function (key) { Object.defineProperty(target, key, Object.getOwnPropertyDescriptor(source, key)); }); } return target; }
var useContext = function useContext(props, setInnerValue, innerPopupVisible, setPopupVisible) {
  var statusContext = reactive({
    treeStore: null,
    inputVal: null,
    scopeVal: void 0,
    treeNodes: [],
    expend: []
  });
  return {
    statusContext: statusContext,
    cascaderContext: computed(function () {
      var size = props.size,
        checkStrictly = props.checkStrictly,
        lazy = props.lazy,
        multiple = props.multiple,
        filterable = props.filterable,
        clearable = props.clearable,
        checkProps = props.checkProps,
        max = props.max,
        disabled = props.disabled,
        showAllLevels = props.showAllLevels,
        minCollapsedNum = props.minCollapsedNum,
        valueType = props.valueType;
      return _objectSpread(_objectSpread({
        value: statusContext.scopeVal,
        size: size,
        checkStrictly: checkStrictly,
        lazy: lazy,
        multiple: multiple,
        filterable: filterable,
        clearable: clearable,
        checkProps: checkProps,
        max: max,
        disabled: disabled,
        showAllLevels: showAllLevels,
        minCollapsedNum: minCollapsedNum,
        valueType: valueType,
        visible: innerPopupVisible.value
      }, statusContext), {}, {
        setTreeNodes: function setTreeNodes(nodes) {
          statusContext.treeNodes = nodes;
        },
        setValue: function setValue(val, source, node) {
          if (isEqual_1(val, statusContext.scopeVal)) return;
          setInnerValue(val, {
            source: source,
            node: node
          });
        },
        setVisible: setPopupVisible,
        setInputVal: function setInputVal(val) {
          statusContext.inputVal = val;
        },
        setExpend: function setExpend(val) {
          statusContext.expend = val;
        }
      });
    })
  };
};
var useCascaderContext = function useCascaderContext(props) {
  var disabled = useFormDisabled();
  var _toRefs = toRefs(props),
    value = _toRefs.value,
    modelValue = _toRefs.modelValue,
    popupVisible = _toRefs.popupVisible;
  var _useVModel = useVModel(value, modelValue, props.defaultValue, props.onChange),
    _useVModel2 = _slicedToArray(_useVModel, 2),
    innerValue = _useVModel2[0],
    setInnerValue = _useVModel2[1];
  var _useDefaultValue = useDefaultValue(popupVisible, false, props.onPopupVisibleChange, "popupVisible"),
    _useDefaultValue2 = _slicedToArray(_useDefaultValue, 2),
    innerPopupVisible = _useDefaultValue2[0],
    setPopupVisible = _useDefaultValue2[1];
  var _useContext = useContext(props, setInnerValue, innerPopupVisible, setPopupVisible),
    cascaderContext = _useContext.cascaderContext,
    statusContext = _useContext.statusContext;
  var isFilterable = computed(function () {
    return Boolean(props.filterable || isFunction_1(props.filter));
  });
  var updatedTreeNodes = function updatedTreeNodes() {
    var _cascaderContext$valu = cascaderContext.value,
      inputVal = _cascaderContext$valu.inputVal,
      treeStore = _cascaderContext$valu.treeStore,
      setTreeNodes = _cascaderContext$valu.setTreeNodes;
    treeNodesEffect(inputVal, treeStore, setTreeNodes, props.filter);
  };
  var updateExpend = function updateExpend() {
    var _cascaderContext$valu2 = cascaderContext.value,
      value2 = _cascaderContext$valu2.value,
      treeStore = _cascaderContext$valu2.treeStore;
    var expend = statusContext.expend;
    treeStoreExpendEffect(treeStore, value2, expend);
    treeStore.replaceChecked(getTreeValue(value2));
  };
  watch(function () {
    return props.options;
  }, function () {
    var options = props.options,
      _props$keys = props.keys,
      keys = _props$keys === void 0 ? {} : _props$keys,
      checkStrictly = props.checkStrictly,
      lazy = props.lazy,
      load = props.load,
      valueMode = props.valueMode;
    var treeStore = statusContext.treeStore;
    if (!options.length && !treeStore) return;
    if (!treeStore) {
      var store = new TreeStore({
        keys: _objectSpread(_objectSpread({}, keys), {}, {
          children: isString_1(keys.children) ? keys.children : "children"
        }),
        checkable: true,
        expandMutex: true,
        expandParent: true,
        lazy: lazy,
        load: load,
        valueMode: valueMode,
        checkStrictly: checkStrictly,
        onLoad: function onLoad() {
          nextTick(function () {
            store.refreshNodes();
            updatedTreeNodes();
          });
        }
      });
      store.append(options);
      statusContext.treeStore = store;
    } else {
      treeStore.reload(options);
      treeStore.refreshNodes();
    }
    updateExpend();
    updatedTreeNodes();
  }, {
    immediate: true,
    deep: true
  });
  watch(function () {
    var checkStrictly = props.checkStrictly,
      lazy = props.lazy,
      load = props.load,
      valueMode = props.valueMode;
    return JSON.stringify({
      valueMode: valueMode,
      checkStrictly: checkStrictly,
      lazy: lazy,
      load: load
    });
  }, function () {
    var treeStore = statusContext.treeStore;
    if (!treeStore) return;
    var checkStrictly = props.checkStrictly,
      lazy = props.lazy,
      load = props.load,
      valueMode = props.valueMode;
    var treeProps = {
      checkStrictly: checkStrictly,
      disabled: disabled,
      load: load,
      lazy: lazy,
      valueMode: valueMode
    };
    treeStore.setConfig(treeProps);
  }, {
    immediate: true
  });
  watch(innerValue, function () {
    var _cascaderContext$valu3 = cascaderContext.value,
      setValue = _cascaderContext$valu3.setValue,
      multiple = _cascaderContext$valu3.multiple,
      valueType = _cascaderContext$valu3.valueType;
    if (isValueInvalid(innerValue.value, cascaderContext.value)) setValue(multiple ? [] : "", "invalid-value");else statusContext.scopeVal = multiple ? [] : "";
    if (!isEmptyValues(innerValue)) statusContext.scopeVal = getCascaderValue(innerValue.value, valueType, multiple);
    if (!statusContext.treeStore) return;
    updateExpend();
    updatedTreeNodes();
  }, {
    immediate: true
  });
  watch(function () {
    return innerPopupVisible.value && isFilterable.value;
  }, function (visible) {
    var setInputVal = cascaderContext.value.setInputVal;
    if (visible) setInputVal("");
  });
  watch(function () {
    return statusContext.inputVal;
  }, function () {
    updatedTreeNodes();
  });
  return {
    cascaderContext: cascaderContext,
    isFilterable: isFilterable
  };
};

export { useCascaderContext, useContext };
//# sourceMappingURL=hooks.mjs.map
