/**
 * ldesign v0.4.2
 * (c) 2023 ldesign
 * @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 vue = require('vue');
var isObject = require('lodash/isObject');
var isFunction = require('lodash/isFunction');
var form_hooks = require('../form/hooks.js');

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 useOverlayInnerStyle(props) {
  var _toRefs = vue.toRefs(props),
    popupProps = _toRefs.popupProps,
    autoWidth = _toRefs.autoWidth;
  var innerPopupVisible = vue.ref(false);
  var disable = form_hooks.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__default["default"](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 = vue.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__default["default"](overlayInnerStyle) || isObject__default["default"](overlayInnerStyle) && overlayInnerStyle.width) result = overlayInnerStyle;else if (!autoWidth.value) result = matchWidthFunc;
    return result;
  });
  return {
    tOverlayInnerStyle: tOverlayInnerStyle,
    innerPopupVisible: innerPopupVisible,
    onInnerPopupVisibleChange: onInnerPopupVisibleChange
  };
}

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