"use strict";
const React = require("react");
const keycode = require("../_util/keycode.js");
var __read = globalThis && globalThis.__read || function(o, n) {
  var m = typeof Symbol === "function" && o[Symbol.iterator];
  if (!m)
    return o;
  var i = m.call(o), r, ar = [], e;
  try {
    while ((n === void 0 || n-- > 0) && !(r = i.next()).done)
      ar.push(r.value);
  } catch (error) {
    e = { error };
  } finally {
    try {
      if (r && !r.done && (m = i["return"]))
        m.call(i);
    } finally {
      if (e)
        throw e.error;
    }
  }
  return ar;
};
function useComposition(_a) {
  var value = _a.value, maxLength = _a.maxLength, onChange = _a.onChange, onKeyDown = _a.onKeyDown, onPressEnter = _a.onPressEnter, beforeTriggerValueChangeCallback = _a.beforeTriggerValueChangeCallback;
  var refIsComposition = React.useRef(false);
  var _b = __read(React.useState(""), 2), compositionValue = _b[0], setCompositionValue = _b[1];
  var triggerValueChangeCallback = function(newValue, e) {
    if (beforeTriggerValueChangeCallback) {
      beforeTriggerValueChangeCallback(newValue);
    }
    if (onChange && newValue !== value && (maxLength === void 0 || newValue.length <= maxLength)) {
      onChange(newValue, e);
    }
  };
  return {
    compositionValue,
    triggerValueChangeCallback,
    compositionHandler: function(e) {
      refIsComposition.current = e.type !== "compositionend";
      if (!refIsComposition.current) {
        setCompositionValue(void 0);
        triggerValueChangeCallback(e.target.value, e);
      }
    },
    valueChangeHandler: function(e) {
      var newValue = e.target.value;
      if (!refIsComposition.current) {
        compositionValue && setCompositionValue(void 0);
        triggerValueChangeCallback(newValue, e);
      } else {
        refIsComposition.current = false;
        setCompositionValue(newValue);
      }
    },
    keyDownHandler: function(e) {
      var keyCode = e.keyCode || e.which;
      if (!refIsComposition.current) {
        onKeyDown && onKeyDown(e);
        if (keyCode === keycode.Enter.code) {
          onPressEnter && onPressEnter(e);
        }
      }
    }
  };
}
module.exports = useComposition;
//# sourceMappingURL=useComposition.js.map
