/**
 * ldesign v0.4.6
 * (c) 2023 ldesign
 * @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 _common_js_tree_treeStore = require('../_common/js/tree/tree-store.js');
var hooks_useVModel = require('../hooks/useVModel.js');
var hooks_useDefaultValue = require('../hooks/useDefaultValue.js');
var cascader_core_helper = require('./core/helper.js');
var cascader_core_effect = require('./core/effect.js');
require('@babel/runtime/helpers/classCallCheck');
require('@babel/runtime/helpers/createClass');
require('lodash/isArray');
require('lodash/isNumber');
require('lodash/isString');
require('lodash/difference');
require('lodash/camelCase');
require('lodash/isPlainObject');
require('mitt');
require('../_common/js/tree/tree-node.js');
require('@babel/runtime/helpers/asyncToGenerator');
require('@babel/runtime/helpers/toConsumableArray');
require('@babel/runtime/regenerator');
require('lodash/isNull');
require('lodash/uniqueId');
require('lodash/isNil');
require('lodash/get');
require('../_common/js/log/log.js');
require('../_common/js/tree/tree-node-model.js');
require('lodash/isUndefined');
require('lodash/isBoolean');
require('lodash/pick');
require('lodash/omit');
require('@babel/runtime/helpers/typeof');
require('lodash/kebabCase');
require('lodash/isEmpty');
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,
        value = props.value;
      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,
        cascaderValue: 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;
    cascader_core_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;
    cascader_core_effect.treeStoreExpendEffect(treeStore, value2, expend);
    treeStore.replaceChecked(cascader_core_helper.getTreeValue(value2));
  };
  var loadChildrenByValue = function loadChildrenByValue() {
    var value2 = props.value,
      multiple = props.multiple;
    var treeStore = statusContext.treeStore;
    if (Array.isArray(value2)) {
      if (!multiple && value2.length > treeStore.expandedMap.size) {
        var expanded = value2.slice(0, treeStore.expandedMap.size + 1);
        treeStore.setExpanded(expanded);
      }
      if (multiple) {
        if (value2.length > 0) {
          var firstValue = value2[0];
          if (firstValue.length > treeStore.expandedMap.size) treeStore.setExpanded(firstValue.slice(0, treeStore.expandedMap.size + 1));
        }
        var lastValues = value2.map(function (item) {
          return item[item.length - 1];
        });
        treeStore.setChecked(lastValues);
      }
    }
  };
  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 = statusContext.treeStore;
    if (!options.length && !treeStore) return;
    if (!treeStore) {
      var store = new _common_js_tree_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();
            loadChildrenByValue();
          });
        }
      });
      store.append(options);
      statusContext.treeStore = store;
    } else {
      treeStore.reload(options);
      treeStore.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 (cascader_core_helper.isValueInvalid(innerValue.value, cascaderContext.value)) setValue(multiple ? [] : "", "invalid-value");
    if (!cascader_core_helper.isEmptyValues(innerValue)) statusContext.scopeVal = cascader_core_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 {
    innerValue: innerValue,
    cascaderContext: cascaderContext,
    isFilterable: isFilterable
  };
};

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