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

'use strict';

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

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 isObject__default = /*#__PURE__*/_interopDefaultLegacy(isObject);
var isFunction__default = /*#__PURE__*/_interopDefaultLegacy(isFunction);

const MAX_POPUP_WIDTH = 1e3;
function useOverlayInnerStyle(props) {
  const instance = VueCompositionAPI.getCurrentInstance();
  const {
    popupProps,
    autoWidth
  } = VueCompositionAPI.toRefs(props);
  const innerPopupVisible = VueCompositionAPI.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 = VueCompositionAPI.computed(() => {
    var _popupProps$value;
    let result = {};
    const overlayInnerStyle = ((_popupProps$value = popupProps.value) === null || _popupProps$value === void 0 ? void 0 : _popupProps$value.overlayInnerStyle) || {};
    if (isFunction__default["default"](overlayInnerStyle) || isObject__default["default"](overlayInnerStyle) && overlayInnerStyle.width) result = overlayInnerStyle;else if (!autoWidth.value) result = matchWidthFunc;
    return result;
  });
  return {
    tOverlayInnerStyle,
    innerPopupVisible,
    onInnerPopupVisibleChange
  };
}

exports["default"] = useOverlayInnerStyle;
//# sourceMappingURL=use-overlay-innerstyle.js.map
