import _objectSpread from "@babel/runtime/helpers/esm/objectSpread2";
import { Fragment as _Fragment, createVNode as _createVNode } from "vue";
import _extends from "@babel/runtime/helpers/esm/extends";
var __awaiter = this && this.__awaiter || function (thisArg, _arguments, P, generator) {
  function adopt(value) {
    return value instanceof P ? value : new P(function (resolve) {
      resolve(value);
    });
  }
  return new (P || (P = Promise))(function (resolve, reject) {
    function fulfilled(value) {
      try {
        step(generator.next(value));
      } catch (e) {
        reject(e);
      }
    }
    function rejected(value) {
      try {
        step(generator["throw"](value));
      } catch (e) {
        reject(e);
      }
    }
    function step(result) {
      result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected);
    }
    step((generator = generator.apply(thisArg, _arguments || [])).next());
  });
};
var __rest = this && this.__rest || function (s, e) {
  var t = {};
  for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p) && e.indexOf(p) < 0) t[p] = s[p];
  if (s != null && typeof Object.getOwnPropertySymbols === "function") for (var i = 0, p = Object.getOwnPropertySymbols(s); i < p.length; i++) {
    if (e.indexOf(p[i]) < 0 && Object.prototype.propertyIsEnumerable.call(s, p[i])) t[p[i]] = s[p[i]];
  }
  return t;
};
import { defineComponent, nextTick, ref } from 'vue';
import Handle from './Handle';
import { getIndex } from '../util';
export const handlesProps = () => ({
  prefixCls: String,
  style: [Object, Array],
  values: Array,
  onStartMove: Function,
  onOffsetChange: Function,
  onFocus: Function,
  onBlur: Function,
  onDelete: Function,
  handleRender: Function,
  activeHandleRender: Function,
  draggingIndex: Number,
  draggingDelete: {
    type: Boolean,
    defaultValue: undefined
  },
  onChangeComplete: Function
});
const Handles = defineComponent({
  name: 'Handles',
  props: handlesProps(),
  setup(props, {
    expose
  }) {
    const handlesRef = ref({});
    // =========================== Active ===========================
    const activeVisible = ref(false);
    const activeIndex = ref(-1);
    const onActive = index => {
      activeVisible.value = true;
      activeIndex.value = index;
    };
    const onHandleFocus = (e, index) => {
      var _a;
      onActive(index);
      (_a = props.onFocus) === null || _a === void 0 ? void 0 : _a.call(props, e);
    };
    const onHandleMouseEnter = (_, index) => {
      onActive(index);
    };
    // =========================== Expose ===========================
    const focus = index => {
      var _a;
      (_a = handlesRef.value[index]) === null || _a === void 0 ? void 0 : _a.focus();
    };
    const hideHelp = () => __awaiter(this, void 0, void 0, function* () {
      activeVisible.value = false;
      yield nextTick();
    });
    expose({
      focus,
      hideHelp
    });
    // =========================== Render ===========================
    return () => {
      const {
          prefixCls,
          style,
          onStartMove,
          onOffsetChange,
          values,
          handleRender,
          activeHandleRender,
          draggingIndex,
          draggingDelete,
          onFocus
        } = props,
        restProps = __rest(props, ["prefixCls", "style", "onStartMove", "onOffsetChange", "values", "handleRender", "activeHandleRender", "draggingIndex", "draggingDelete", "onFocus"]);
      const handleProps = _extends({
        prefixCls,
        onStartMove,
        onOffsetChange,
        render: handleRender,
        onFocus: onHandleFocus,
        onMouseEnter: onHandleMouseEnter
      }, restProps);
      return _createVNode(_Fragment, null, [values.map((value, index) => {
        const dragging = draggingIndex === index;
        return _createVNode(Handle, _objectSpread({
          "ref": node => {
            if (!node) {
              delete handlesRef.value[index];
            } else {
              handlesRef.value[index] = node;
            }
          },
          "dragging": dragging,
          "draggingDelete": dragging && draggingDelete,
          "style": getIndex(style, index),
          "key": index,
          "value": value,
          "valueIndex": index
        }, handleProps), null);
      }), activeHandleRender && activeVisible && _createVNode(Handle, _objectSpread(_objectSpread({
        "key": "a11y"
      }, handleProps), {}, {
        "value": values[activeIndex.value],
        "valueIndex": null,
        "dragging": draggingIndex !== -1,
        "draggingDelete": draggingDelete,
        "render": activeHandleRender,
        "style": {
          pointerEvents: 'none'
        },
        "tab-index": null,
        "aria-hidden": true
      }), null)]);
    };
  }
});
export default Handles;