/**
 * ldesign v0.2.14
 * (c) 2022/11/2 16:16 built by swimly
 * @license MIT
 */

import { getCurrentInstance, toRefs, ref, computed } from '@vue/composition-api';
import isObject from 'lodash/isObject';
import isFunction from 'lodash/isFunction';

const MAX_POPUP_WIDTH = 1e3;
function useOverlayInnerStyle(props) {
  const instance = getCurrentInstance();
  const {
    popupProps,
    autoWidth
  } = toRefs(props);
  const innerPopupVisible = ref(false);
  const matchWidthFunc = (triggerElement, popupElement) => {
    const SCROLLBAR_WIDTH = popupElement.scrollHeight > popupElement.offsetHeight ? 8 : 0;
    const width = popupElement.offsetWidth + SCROLLBAR_WIDTH >= triggerElement.offsetWidth ? popupElement.offsetWidth : triggerElement.offsetWidth;
    let otherOverlayInnerStyle = {};
    if (popupProps.value && typeof popupProps.value.overlayInnerStyle === "object" && !popupProps.value.overlayInnerStyle.width) otherOverlayInnerStyle = popupProps.value.overlayInnerStyle;
    return {
      width: `${Math.min(width, MAX_POPUP_WIDTH)}px`,
      ...otherOverlayInnerStyle
    };
  };
  const onInnerPopupVisibleChange = (visible, context) => {
    var _props$onPopupVisible;
    if (props.disabled || props.readonly) return;
    const newVisible = context.trigger === "trigger-element-click" && props.allowInput ? 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);
  };
  const tOverlayInnerStyle = computed(() => {
    var _popupProps$value;
    let result = {};
    const 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,
    innerPopupVisible,
    onInnerPopupVisibleChange
  };
}

export { useOverlayInnerStyle as default };
//# sourceMappingURL=use-overlay-innerstyle.js.map
