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

'use strict';

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

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

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

var _defineProperty__default = /*#__PURE__*/_interopDefaultLegacy(_defineProperty);
var _typeof__default = /*#__PURE__*/_interopDefaultLegacy(_typeof);
var isObject__default = /*#__PURE__*/_interopDefaultLegacy(isObject);
var isFunction__default = /*#__PURE__*/_interopDefaultLegacy(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__default["default"](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 = VueCompositionAPI.getCurrentInstance();

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

  var innerPopupVisible = VueCompositionAPI.ref(false);
  var tOverlayStyle = VueCompositionAPI.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__default["default"](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);
  };

  VueCompositionAPI.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__default["default"](overlayStyle) || isObject__default["default"](overlayStyle) && overlayStyle.width) {
      result = overlayStyle;
    } else if (!autoWidth.value) {
      result = macthWidthFunc;
    }

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

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