/**
 * ldesign v0.2.2
 * (c) 2022年9月20日 swimly
 * @license MIT
 */

'use strict';

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

var _slicedToArray = require('@babel/runtime/helpers/slicedToArray');
var _defineProperty = require('@babel/runtime/helpers/defineProperty');
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('@babel/runtime/helpers/classCallCheck');
require('@babel/runtime/helpers/createClass');
require('../../../chunks/defineProperty-28395935.js');
require('../../../chunks/tree-node.js');
require('@babel/runtime/helpers/toConsumableArray');
require('@babel/runtime/regenerator');
require('@babel/runtime/helpers/typeof');
require('../../../chunks/isArray-b0f0e351.js');
require('../../../chunks/_isIndex-1bf92538.js');
require('../../../chunks/isObjectLike-2443c6ff.js');
require('../../../chunks/_commonjsHelpers-ea2bee21.js');
require('../../../chunks/log.js');
require('lodash/isEmpty');
require('lodash/isNumber');
require('lodash/cloneDeep');

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

var _slicedToArray__default = /*#__PURE__*/_interopDefaultLegacy(_slicedToArray);
var _defineProperty__default = /*#__PURE__*/_interopDefaultLegacy(_defineProperty);
var isEqual__default = /*#__PURE__*/_interopDefaultLegacy(isEqual);
var isFunction__default = /*#__PURE__*/_interopDefaultLegacy(isFunction);

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__default["default"](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 = VueCompositionAPI.reactive({
    treeStore: null,
    inputVal: null,
    scopeVal: void 0,
    treeNodes: [],
    expend: []
  });
  return {
    statusContext: statusContext,
    cascaderContext: VueCompositionAPI.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__default["default"](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 = props.disabled;

  var _toRefs = VueCompositionAPI.toRefs(props),
      value = _toRefs.value,
      popupVisible = _toRefs.popupVisible;

  var _useVModel = hooks_useVmodel["default"](value, props.defaultValue, props.onChange, "change"),
      _useVModel2 = _slicedToArray__default["default"](_useVModel, 2),
      innerValue = _useVModel2[0],
      setInnerValue = _useVModel2[1];

  var _useDefaultValue = hooks_useDefaultValue["default"](popupVisible, false, props.onPopupVisibleChange, "popupVisible", "popup-visible-change"),
      _useDefaultValue2 = _slicedToArray__default["default"](_useDefaultValue, 2),
      innerPopupVisible = _useDefaultValue2[0],
      setPopupVisible = _useDefaultValue2[1];

  var _useContext = useContext(props, setInnerValue, innerPopupVisible, setPopupVisible),
      cascaderContext = _useContext.cascaderContext,
      statusContext = _useContext.statusContext;

  var isFilterable = VueCompositionAPI.computed(function () {
    return Boolean(props.filterable || isFunction__default["default"](props.filter));
  });

  var updatedTreeNodes = function updatedTreeNodes() {
    var _cascaderContext$valu = cascaderContext.value,
        inputVal = _cascaderContext$valu.inputVal,
        treeStore = _cascaderContext$valu.treeStore,
        setTreeNodes = _cascaderContext$valu.setTreeNodes;
    components_cascader_utils_effect.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;
    components_cascader_utils_effect.treeStoreExpendEffect(treeStore, value2, expend);
    treeStore.replaceChecked(components_cascader_utils_helper.getTreeValue(value2));
  };

  VueCompositionAPI.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$1 = statusContext.treeStore;
    if (!options.length && !treeStore$1) return;

    if (!treeStore$1) {
      var store = new treeStore.TreeStore({
        keys: _objectSpread(_objectSpread({}, keys), {}, {
          children: typeof keys.children === "string" ? keys.children : "children"
        }),
        checkable: true,
        expandMutex: true,
        expandParent: true,
        checkStrictly: checkStrictly,
        lazy: lazy,
        load: load,
        valueMode: valueMode,
        onLoad: function onLoad() {
          VueCompositionAPI.nextTick(function () {
            store.refreshNodes();
            updatedTreeNodes();
          });
        }
      });
      store.append(options);
      statusContext.treeStore = store;
    } else {
      treeStore$1.reload(options);
      treeStore$1.refreshNodes();
    }

    updateExpend();
    updatedTreeNodes();
  }, {
    immediate: true
  });
  VueCompositionAPI.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
  });
  VueCompositionAPI.watch(innerValue, function () {
    var _cascaderContext$valu3 = cascaderContext.value,
        setValue = _cascaderContext$valu3.setValue,
        multiple = _cascaderContext$valu3.multiple,
        valueType = _cascaderContext$valu3.valueType;

    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(function () {
    return innerPopupVisible.value && isFilterable.value;
  }, function (visible) {
    var setInputVal = cascaderContext.value.setInputVal;

    if (visible) {
      setInputVal("");
    }
  });
  VueCompositionAPI.watch(function () {
    return statusContext.inputVal;
  }, function () {
    updatedTreeNodes();
  });
  return {
    cascaderContext: cascaderContext,
    isFilterable: isFilterable
  };
};

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