'use strict';

var vue = require('vue');

function useMergedState(defaultStateValue, option) {
  const { defaultValue, value = vue.ref() } = option || {};
  let initValue = typeof defaultStateValue === "function" ? defaultStateValue() : defaultStateValue;
  if (value.value !== void 0)
    initValue = vue.unref(value);
  if (defaultValue !== void 0)
    initValue = typeof defaultValue === "function" ? defaultValue() : defaultValue;
  const innerValue = vue.ref(initValue);
  const mergedValue = vue.ref(initValue);
  vue.watchEffect(() => {
    let val = value.value !== void 0 ? value.value : innerValue.value;
    if (option == null ? void 0 : option.postState)
      val = option.postState(val);
    mergedValue.value = val;
  });
  function triggerChange(newValue) {
    const preVal = mergedValue.value;
    innerValue.value = newValue;
    if (vue.toRaw(mergedValue.value) !== newValue && (option == null ? void 0 : option.onChange))
      option.onChange(newValue, preVal);
  }
  vue.watch(value, () => {
    innerValue.value = value.value;
  });
  return [mergedValue, triggerChange];
}

exports.useMergedState = useMergedState;
//# sourceMappingURL=index.cjs.map
