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

import _defineProperty from '@babel/runtime/helpers/defineProperty';
import _typeof from '@babel/runtime/helpers/typeof';
import { toRefs, ref, computed } from 'vue';
import isObject from 'lodash/isObject';
import isFunction from 'lodash/isFunction';
import { useFormDisabled } from '../form/hooks.js';

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 useOverlayInnerStyle(props) {
  var _toRefs = toRefs(props),
    popupProps = _toRefs.popupProps,
    autoWidth = _toRefs.autoWidth;
  var innerPopupVisible = ref(false);
  var disable = useFormDisabled();
  var matchWidthFunc = function matchWidthFunc(triggerElement, popupElement) {
    var SCROLLBAR_WIDTH = popupElement.scrollHeight > popupElement.offsetHeight ? 8 : 0;
    var width = popupElement.offsetWidth + SCROLLBAR_WIDTH >= triggerElement.offsetWidth ? popupElement.offsetWidth : triggerElement.offsetWidth;
    var otherOverlayInnerStyle = {};
    if (popupProps.value && _typeof(popupProps.value.overlayInnerStyle) === "object" && !popupProps.value.overlayInnerStyle.width) otherOverlayInnerStyle = popupProps.value.overlayInnerStyle;
    return _objectSpread({
      width: "".concat(Math.min(width, MAX_POPUP_WIDTH), "px")
    }, otherOverlayInnerStyle);
  };
  var onInnerPopupVisibleChange = function onInnerPopupVisibleChange(visible, context) {
    if (disable.value || props.readonly) return;
    var newVisible = context.trigger === "trigger-element-click" && props.allowInput ? true : visible;
    if (props.popupVisible !== newVisible) {
      var _props$onPopupVisible;
      innerPopupVisible.value = newVisible;
      (_props$onPopupVisible = props.onPopupVisibleChange) === null || _props$onPopupVisible === void 0 ? void 0 : _props$onPopupVisible.call(props, newVisible, context);
    }
  };
  var tOverlayInnerStyle = computed(function () {
    var _popupProps$value;
    var result = {};
    var overlayInnerStyle = ((_popupProps$value = popupProps.value) === null || _popupProps$value === void 0 ? void 0 : _popupProps$value.overlayInnerStyle) || {};
    if (isFunction(overlayInnerStyle) || isObject(overlayInnerStyle) && overlayInnerStyle.width) result = overlayInnerStyle;else if (!autoWidth.value) result = matchWidthFunc;
    return result;
  });
  return {
    tOverlayInnerStyle: tOverlayInnerStyle,
    innerPopupVisible: innerPopupVisible,
    onInnerPopupVisibleChange: onInnerPopupVisibleChange
  };
}

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