'use strict';

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

var vue = require('vue');
require('../../../../utils/index.cjs');
var uiUtil = require('../utils/uiUtil.cjs');
var KeyCode = require('../../../../utils/KeyCode.cjs');
var raf = require('../../../../utils/vue/raf.cjs');

function usePickerInput({
  open,
  value,
  isClickOutside,
  triggerOpen,
  forwardKeydown,
  onKeydown,
  blurToCancel,
  onSubmit,
  onCancel,
  onFocus,
  onBlur
}) {
  const typing = vue.shallowRef(false);
  const focused = vue.shallowRef(false);
  const preventBlurRef = vue.shallowRef(false);
  const valueChangedRef = vue.shallowRef(false);
  const preventDefaultRef = vue.shallowRef(false);
  const inputProps = vue.computed(() => ({
    onMousedown: () => {
      typing.value = true;
      triggerOpen(true);
    },
    onKeydown: (e) => {
      const preventDefault = () => {
        preventDefaultRef.value = true;
      };
      onKeydown(e, preventDefault);
      if (preventDefaultRef.value)
        return;
      switch (e.which) {
        case KeyCode.KeyCode.ENTER: {
          if (!open.value)
            triggerOpen(true);
          else if (onSubmit() !== false)
            typing.value = true;
          e.preventDefault();
          return;
        }
        case KeyCode.KeyCode.TAB: {
          if (typing.value && open.value && !e.shiftKey) {
            typing.value = false;
            e.preventDefault();
          } else if (!typing.value && open.value) {
            if (!forwardKeydown(e) && e.shiftKey) {
              typing.value = true;
              e.preventDefault();
            }
          }
          return;
        }
        case KeyCode.KeyCode.ESC: {
          typing.value = true;
          onCancel();
          return;
        }
      }
      if (!open.value && ![KeyCode.KeyCode.SHIFT].includes(e.which)) {
        triggerOpen(true);
      } else if (!typing.value) {
        forwardKeydown(e);
      }
    },
    onFocus: (e) => {
      typing.value = true;
      focused.value = true;
      if (onFocus)
        onFocus(e);
    },
    onBlur: (e) => {
      if (preventBlurRef.value || !isClickOutside(document.activeElement)) {
        preventBlurRef.value = false;
        return;
      }
      if (blurToCancel.value) {
        setTimeout(() => {
          let { activeElement } = document;
          while (activeElement && activeElement.shadowRoot)
            activeElement = activeElement.shadowRoot.activeElement;
          if (isClickOutside(activeElement))
            onCancel();
        }, 0);
      } else if (open.value) {
        triggerOpen(false);
        if (valueChangedRef.value)
          onSubmit();
      }
      focused.value = false;
      if (onBlur)
        onBlur(e);
    }
  }));
  vue.watch(open, () => {
    valueChangedRef.value = false;
  });
  vue.watch(value, () => {
    valueChangedRef.value = true;
  });
  const globalMousedownEvent = vue.shallowRef();
  vue.onMounted(() => {
    globalMousedownEvent.value = uiUtil.addGlobalMousedownEvent((e) => {
      const target = uiUtil.getTargetFromEvent(e);
      if (open.value) {
        const clickedOutside = isClickOutside(target);
        if (!clickedOutside) {
          preventBlurRef.value = true;
          raf.raf(() => {
            preventBlurRef.value = false;
          });
        } else if (!focused.value || clickedOutside) {
          triggerOpen(false);
        }
      }
    });
  });
  vue.onBeforeUnmount(() => {
    globalMousedownEvent.value && globalMousedownEvent.value();
  });
  return [inputProps, { focused, typing }];
}

exports.default = usePickerInput;
//# sourceMappingURL=usePickerInput.cjs.map
