/**
 * tdesign v0.39.1-alpha.1
 * (c) 2022 tdesign
 * @license MIT
 */

import _defineProperty from '@babel/runtime/helpers/defineProperty';
import _typeof from '@babel/runtime/helpers/typeof';
import { getCurrentInstance, toRefs, ref, watch } from '@vue/composition-api';
import isObject from 'lodash/isObject';
import isFunction from 'lodash/isFunction';

function ownKeys(object, enumerableOnly) { var keys = Object.keys(object); if (Object.getOwnPropertySymbols) { var symbols = Object.getOwnPropertySymbols(object); enumerableOnly && (symbols = symbols.filter(function (sym) { return Object.getOwnPropertyDescriptor(object, sym).enumerable; })), keys.push.apply(keys, symbols); } return keys; }

function _objectSpread(target) { for (var i = 1; i < arguments.length; i++) { var source = null != arguments[i] ? arguments[i] : {}; i % 2 ? ownKeys(Object(source), !0).forEach(function (key) { _defineProperty(target, key, source[key]); }) : Object.getOwnPropertyDescriptors ? Object.defineProperties(target, Object.getOwnPropertyDescriptors(source)) : ownKeys(Object(source)).forEach(function (key) { Object.defineProperty(target, key, Object.getOwnPropertyDescriptor(source, key)); }); } return target; }
var MAX_POPUP_WIDTH = 1e3;
function useOverlayStyle(props) {
  var instance = getCurrentInstance();

  var _toRefs = toRefs(props),
      popupProps = _toRefs.popupProps,
      autoWidth = _toRefs.autoWidth;

  var innerPopupVisible = ref(false);
  var tOverlayStyle = ref();

  var macthWidthFunc = function macthWidthFunc(triggerElement, popupElement) {
    var SCROLLBAR_WIDTH = popupElement.scrollHeight > popupElement.offsetHeight ? 8 : 0;
    var width = popupElement.offsetWidth + SCROLLBAR_WIDTH >= triggerElement.offsetWidth ? popupElement.offsetWidth : triggerElement.offsetWidth;
    var otherOverlayStyle = {};

    if (popupProps.value && _typeof(popupProps.value.overlayStyle) === "object" && !popupProps.value.overlayStyle.width) {
      otherOverlayStyle = popupProps.value.overlayStyle;
    }

    return _objectSpread({
      width: "".concat(Math.min(width, MAX_POPUP_WIDTH), "px")
    }, otherOverlayStyle);
  };

  var onInnerPopupVisibleChange = function onInnerPopupVisibleChange(visible, context) {
    var _props$onPopupVisible;

    if (props.disabled || props.readonly) return;
    var newVisible = context.trigger === "trigger-element-click" ? true : visible;
    innerPopupVisible.value = newVisible;
    (_props$onPopupVisible = props.onPopupVisibleChange) === null || _props$onPopupVisible === void 0 ? void 0 : _props$onPopupVisible.call(props, newVisible, context);
    instance.emit("popup-visible-change", newVisible, context);
  };

  watch([innerPopupVisible, popupProps], function () {
    var _popupProps$value;

    if (tOverlayStyle.value) return;
    var result = {};
    var overlayStyle = ((_popupProps$value = popupProps.value) === null || _popupProps$value === void 0 ? void 0 : _popupProps$value.overlayStyle) || {};

    if (isFunction(overlayStyle) || isObject(overlayStyle) && overlayStyle.width) {
      result = overlayStyle;
    } else if (!autoWidth.value) {
      result = macthWidthFunc;
    }

    tOverlayStyle.value = result;
  });
  return {
    tOverlayStyle: tOverlayStyle,
    innerPopupVisible: innerPopupVisible,
    onInnerPopupVisibleChange: onInnerPopupVisibleChange
  };
}

export { useOverlayStyle as default };
//# sourceMappingURL=useOverlayStyle.js.map
