"use strict";

var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");

Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.default = void 0;

var _vue = require("vue");

var _objectSpread2 = _interopRequireDefault(require("@babel/runtime/helpers/objectSpread2"));

var _toConsumableArray2 = _interopRequireDefault(require("@babel/runtime/helpers/toConsumableArray"));

var _Context = require("./Context");

var _props = require("./props");

var _useMemo = _interopRequireDefault(require("../_util/hooks/useMemo"));

var _KeyCode = _interopRequireDefault(require("../_util/KeyCode"));

var _Tree = _interopRequireDefault(require("../vc-tree/Tree"));

var HIDDEN_STYLE = {
  width: 0,
  height: 0,
  display: 'flex',
  overflow: 'hidden',
  opacity: 0,
  border: 0,
  padding: 0,
  margin: 0
};

var _default = (0, _vue.defineComponent)({
  name: 'OptionList',
  inheritAttrs: false,
  props: (0, _props.optionListProps)(),
  slots: ['notFoundContent', 'menuItemSelectedIcon'],
  setup: function setup(props, _ref) {
    var slots = _ref.slots,
        expose = _ref.expose;
    var context = (0, _Context.useInjectTreeSelectContext)();
    var treeRef = (0, _vue.ref)();
    var memoOptions = (0, _useMemo.default)(function () {
      return props.options;
    }, [function () {
      return props.open;
    }, function () {
      return props.options;
    }], function (next, prev) {
      return next[0] && prev[1] !== next[1];
    });
    var valueKeys = (0, _vue.computed)(function () {
      var _context$value = context.value,
          checkedKeys = _context$value.checkedKeys,
          getEntityByValue = _context$value.getEntityByValue;
      return checkedKeys.map(function (val) {
        var entity = getEntityByValue(val);
        return entity ? entity.key : null;
      });
    });
    var mergedCheckedKeys = (0, _vue.computed)(function () {
      var _context$value2 = context.value,
          checkable = _context$value2.checkable,
          halfCheckedKeys = _context$value2.halfCheckedKeys;

      if (!checkable) {
        return null;
      }

      return {
        checked: valueKeys.value,
        halfChecked: halfCheckedKeys
      };
    });
    (0, _vue.watch)(function () {
      return props.open;
    }, function () {
      (0, _vue.nextTick)(function () {
        if (props.open && !props.multiple && valueKeys.value.length) {
          var _treeRef$value;

          (_treeRef$value = treeRef.value) === null || _treeRef$value === void 0 ? void 0 : _treeRef$value.scrollTo({
            key: valueKeys.value[0]
          });
        }
      });
    }, {
      immediate: true,
      flush: 'post'
    }); // ========================== Search ==========================

    var lowerSearchValue = (0, _vue.computed)(function () {
      return String(props.searchValue).toLowerCase();
    });

    var filterTreeNode = function filterTreeNode(treeNode) {
      if (!lowerSearchValue.value) {
        return false;
      }

      return String(treeNode[context.value.treeNodeFilterProp]).toLowerCase().includes(lowerSearchValue.value);
    }; // =========================== Keys ===========================


    var expandedKeys = (0, _vue.ref)(context.value.treeDefaultExpandedKeys);
    var searchExpandedKeys = (0, _vue.ref)(null);
    (0, _vue.watch)(function () {
      return props.searchValue;
    }, function () {
      if (props.searchValue) {
        searchExpandedKeys.value = props.flattenOptions.map(function (o) {
          return o.key;
        });
      }
    }, {
      immediate: true
    });
    var mergedExpandedKeys = (0, _vue.computed)(function () {
      if (context.value.treeExpandedKeys) {
        return (0, _toConsumableArray2.default)(context.value.treeExpandedKeys);
      }

      return props.searchValue ? searchExpandedKeys.value : expandedKeys.value;
    });

    var onInternalExpand = function onInternalExpand(keys) {
      var _context$value$onTree, _context$value3;

      expandedKeys.value = keys;
      searchExpandedKeys.value = keys;
      (_context$value$onTree = (_context$value3 = context.value).onTreeExpand) === null || _context$value$onTree === void 0 ? void 0 : _context$value$onTree.call(_context$value3, keys);
    }; // ========================== Events ==========================


    var onListMouseDown = function onListMouseDown(event) {
      event.preventDefault();
    };

    var onInternalSelect = function onInternalSelect(_, _ref2) {
      var key = _ref2.node.key;
      var _context$value4 = context.value,
          getEntityByKey = _context$value4.getEntityByKey,
          checkable = _context$value4.checkable,
          checkedKeys = _context$value4.checkedKeys;
      var entity = getEntityByKey(key, checkable ? 'checkbox' : 'select');

      if (entity !== null) {
        var _props$onSelect;

        (_props$onSelect = props.onSelect) === null || _props$onSelect === void 0 ? void 0 : _props$onSelect.call(props, entity.data.value, {
          selected: !checkedKeys.includes(entity.data.value)
        });
      }

      if (!props.multiple) {
        var _props$onToggleOpen;

        (_props$onToggleOpen = props.onToggleOpen) === null || _props$onToggleOpen === void 0 ? void 0 : _props$onToggleOpen.call(props, false);
      }
    }; // ========================= Keyboard =========================


    var activeKey = (0, _vue.ref)(null);
    var activeEntity = (0, _vue.computed)(function () {
      return context.value.getEntityByKey(activeKey.value);
    });

    var setActiveKey = function setActiveKey(key) {
      activeKey.value = key;
    };

    expose({
      scrollTo: function scrollTo() {
        var _treeRef$value2, _treeRef$value2$scrol;

        for (var _len = arguments.length, args = new Array(_len), _key = 0; _key < _len; _key++) {
          args[_key] = arguments[_key];
        }

        return (_treeRef$value2 = treeRef.value) === null || _treeRef$value2 === void 0 ? void 0 : (_treeRef$value2$scrol = _treeRef$value2.scrollTo) === null || _treeRef$value2$scrol === void 0 ? void 0 : _treeRef$value2$scrol.call.apply(_treeRef$value2$scrol, [_treeRef$value2].concat(args));
      },
      onKeydown: function onKeydown(event) {
        var _treeRef$value3;

        var which = event.which;

        switch (which) {
          // >>> Arrow keys
          case _KeyCode.default.UP:
          case _KeyCode.default.DOWN:
          case _KeyCode.default.LEFT:
          case _KeyCode.default.RIGHT:
            (_treeRef$value3 = treeRef.value) === null || _treeRef$value3 === void 0 ? void 0 : _treeRef$value3.onKeydown(event);
            break;
          // >>> Select item

          case _KeyCode.default.ENTER:
            {
              var _activeEntity$value;

              var _ref3 = ((_activeEntity$value = activeEntity.value) === null || _activeEntity$value === void 0 ? void 0 : _activeEntity$value.data.node) || {},
                  selectable = _ref3.selectable,
                  value = _ref3.value;

              if (selectable !== false) {
                onInternalSelect(null, {
                  node: {
                    key: activeKey.value
                  },
                  selected: !context.value.checkedKeys.includes(value)
                });
              }

              break;
            }
          // >>> Close

          case _KeyCode.default.ESC:
            {
              props.onToggleOpen(false);
            }
        }
      },
      onKeyup: function onKeyup() {}
    });
    return function () {
      var _slots$notFoundConten;

      var prefixCls = props.prefixCls,
          height = props.height,
          itemHeight = props.itemHeight,
          virtual = props.virtual,
          multiple = props.multiple,
          searchValue = props.searchValue,
          open = props.open,
          _props$notFoundConten = props.notFoundContent,
          notFoundContent = _props$notFoundConten === void 0 ? (_slots$notFoundConten = slots.notFoundContent) === null || _slots$notFoundConten === void 0 ? void 0 : _slots$notFoundConten.call(slots) : _props$notFoundConten,
          onMouseenter = props.onMouseenter;
      var _context$value5 = context.value,
          checkable = _context$value5.checkable,
          treeDefaultExpandAll = _context$value5.treeDefaultExpandAll,
          treeIcon = _context$value5.treeIcon,
          showTreeIcon = _context$value5.showTreeIcon,
          switcherIcon = _context$value5.switcherIcon,
          treeLine = _context$value5.treeLine,
          loadData = _context$value5.loadData,
          treeLoadedKeys = _context$value5.treeLoadedKeys,
          treeMotion = _context$value5.treeMotion,
          onTreeLoad = _context$value5.onTreeLoad; // ========================== Render ==========================

      if (memoOptions.value.length === 0) {
        return (0, _vue.createVNode)("div", {
          "role": "listbox",
          "class": "".concat(prefixCls, "-empty"),
          "onMousedown": onListMouseDown
        }, [notFoundContent]);
      }

      var treeProps = {};

      if (treeLoadedKeys) {
        treeProps.loadedKeys = treeLoadedKeys;
      }

      if (mergedExpandedKeys.value) {
        treeProps.expandedKeys = mergedExpandedKeys.value;
      }

      return (0, _vue.createVNode)("div", {
        "onMousedown": onListMouseDown,
        "onMouseenter": onMouseenter
      }, [activeEntity.value && open && (0, _vue.createVNode)("span", {
        "style": HIDDEN_STYLE,
        "aria-live": "assertive"
      }, [activeEntity.value.data.value]), (0, _vue.createVNode)(_Tree.default, (0, _objectSpread2.default)((0, _objectSpread2.default)({
        "ref": treeRef,
        "focusable": false,
        "prefixCls": "".concat(prefixCls, "-tree"),
        "treeData": memoOptions.value,
        "height": height,
        "itemHeight": itemHeight,
        "virtual": virtual,
        "multiple": multiple,
        "icon": treeIcon,
        "showIcon": showTreeIcon,
        "switcherIcon": switcherIcon,
        "showLine": treeLine,
        "loadData": searchValue ? null : loadData,
        "motion": treeMotion,
        "checkable": checkable,
        "checkStrictly": true,
        "checkedKeys": mergedCheckedKeys.value,
        "selectedKeys": !checkable ? valueKeys.value : [],
        "defaultExpandAll": treeDefaultExpandAll
      }, treeProps), {}, {
        "onActiveChange": setActiveKey,
        "onSelect": onInternalSelect,
        "onCheck": onInternalSelect,
        "onExpand": onInternalExpand,
        "onLoad": onTreeLoad,
        "filterTreeNode": filterTreeNode
      }), (0, _objectSpread2.default)((0, _objectSpread2.default)({}, slots), {}, {
        checkable: context.value.customCheckable
      }))]);
    };
  }
});

exports.default = _default;