/**
 * ldesign v0.4.6
 * (c) 2023 ldesign
 * @license MIT
 */

'use strict';

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

var _typeof = require('@babel/runtime/helpers/typeof');
var VueCompositionAPI = require('@vue/composition-api');
var kebabCase = require('lodash/kebabCase');

function _interopDefaultLegacy (e) { return e && typeof e === 'object' && 'default' in e ? e : { 'default': e }; }

var _typeof__default = /*#__PURE__*/_interopDefaultLegacy(_typeof);
var kebabCase__default = /*#__PURE__*/_interopDefaultLegacy(kebabCase);

function useVModel(value, defaultValue, onChange) {
  var eventName = arguments.length > 3 && arguments[3] !== undefined ? arguments[3] : "change";
  var propName = arguments.length > 4 && arguments[4] !== undefined ? arguments[4] : "value";
  var alias = arguments.length > 5 && arguments[5] !== undefined ? arguments[5] : [];
  var _getCurrentInstance = VueCompositionAPI.getCurrentInstance(),
    emit = _getCurrentInstance.emit,
    vnode = _getCurrentInstance.vnode;
  var internalValue = VueCompositionAPI.ref();
  internalValue.value = defaultValue;
  var isControlled = Object.prototype.hasOwnProperty.call(vnode.componentOptions.propsData, propName) || Object.prototype.hasOwnProperty.call(vnode.componentOptions.propsData, kebabCase__default["default"](propName));
  if (isControlled) {
    return [value, function (newValue) {
      for (var _len = arguments.length, args = new Array(_len > 1 ? _len - 1 : 0), _key = 1; _key < _len; _key++) {
        args[_key - 1] = arguments[_key];
      }
      emit === null || emit === void 0 ? void 0 : emit.apply(void 0, ["input", newValue].concat(args));
      onChange === null || onChange === void 0 ? void 0 : onChange.apply(void 0, [newValue].concat(args));
      if (eventName && eventName !== "input") emit === null || emit === void 0 ? void 0 : emit.apply(void 0, [eventName, newValue].concat(args));
    }];
  }
  var _loop = function _loop() {
    var item = alias[i];
    if (Object.prototype.hasOwnProperty.call(vnode.componentOptions.propsData, item.propName)) {
      return {
        v: [item.value, function (newValue) {
          for (var _len3 = arguments.length, args = new Array(_len3 > 1 ? _len3 - 1 : 0), _key3 = 1; _key3 < _len3; _key3++) {
            args[_key3 - 1] = arguments[_key3];
          }
          emit === null || emit === void 0 ? void 0 : emit.apply(void 0, ["update:".concat(item.propName), newValue].concat(args));
          onChange === null || onChange === void 0 ? void 0 : onChange.apply(void 0, [newValue].concat(args));
          if (item.eventName && item.eventName !== "input") emit === null || emit === void 0 ? void 0 : emit.apply(void 0, [item.eventName, newValue].concat(args));
        }]
      };
    }
  };
  for (var i = 0, len = alias.length; i < len; i++) {
    var _ret = _loop();
    if (_typeof__default["default"](_ret) === "object") return _ret.v;
  }
  return [internalValue, function (newValue) {
    internalValue.value = newValue;
    for (var _len2 = arguments.length, args = new Array(_len2 > 1 ? _len2 - 1 : 0), _key2 = 1; _key2 < _len2; _key2++) {
      args[_key2 - 1] = arguments[_key2];
    }
    onChange === null || onChange === void 0 ? void 0 : onChange.apply(void 0, [newValue].concat(args));
    if (eventName && eventName !== "input") emit === null || emit === void 0 ? void 0 : emit.apply(void 0, [eventName, newValue].concat(args));
  }];
}

exports["default"] = useVModel;
//# sourceMappingURL=useVModel.js.map
