/**
 * amis-ui v6.12.0-beta.0
 * Copyright 2018-2025 fex
 */

'use strict';

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

var tslib = require('tslib');
var React = require('react');
var amisCore = require('amis-core');
var PopUp = require('./PopUp.js');
var ReactDOM = require('react-dom');
var isNumber = require('lodash/isNumber');

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

var React__default = /*#__PURE__*/_interopDefaultLegacy(React);
var isNumber__default = /*#__PURE__*/_interopDefaultLegacy(isNumber);

var __react_jsx__ = require('react');
var _J$X_ = (__react_jsx__["default"] || __react_jsx__).createElement;
(__react_jsx__["default"] || __react_jsx__).Fragment;
var PopOverContainer = /** @class */ (function (_super) {
    tslib.__extends(PopOverContainer, _super);
    function PopOverContainer() {
        var _this = _super !== null && _super.apply(this, arguments) || this;
        _this.state = {
            isOpened: false
        };
        return _this;
    }
    PopOverContainer.prototype.targetRef = function (target) {
        this.target = target ? ReactDOM.findDOMNode(target) : null;
    };
    PopOverContainer.prototype.handleClick = function (e) {
        var _this = this;
        e === null || e === void 0 ? void 0 : e.preventDefault();
        this.props.disabled ||
            this.setState({
                isOpened: true
            }, function () {
                var _a, _b;
                (_b = (_a = _this.props).onOpen) === null || _b === void 0 ? void 0 : _b.call(_a);
            });
    };
    PopOverContainer.prototype.close = function () {
        var _a = this.props, onBeforeHide = _a.onBeforeHide, onAfterHide = _a.onAfterHide;
        if (onBeforeHide && typeof onBeforeHide === 'function') {
            onBeforeHide === null || onBeforeHide === void 0 ? void 0 : onBeforeHide();
        }
        this.setState({
            isOpened: false
        });
        if (onAfterHide && typeof onAfterHide === 'function') {
            onAfterHide === null || onAfterHide === void 0 ? void 0 : onAfterHide();
        }
    };
    PopOverContainer.prototype.getTarget = function () {
        return this.target || ReactDOM.findDOMNode(this);
    };
    PopOverContainer.prototype.getParent = function () {
        var _a;
        return (_a = this.getTarget()) === null || _a === void 0 ? void 0 : _a.parentElement;
    };
    PopOverContainer.prototype.onConfirm = function () {
        var _a, _b;
        (_b = (_a = this.props).onConfirm) === null || _b === void 0 ? void 0 : _b.call(_a);
        this.close();
    };
    PopOverContainer.calcOverlayWidth = function (overlay, targetWidth) {
        var overlayWidth = overlay && overlay.width;
        if (!overlayWidth || !isNumber__default["default"](targetWidth) || targetWidth < 1)
            return;
        // 数字字符串需要转化下，否则不生效
        if (typeof overlayWidth === 'number' || /^\d+$/.test(overlayWidth)) {
            return amisCore.toNumber(overlayWidth);
        }
        // 带单位，如: 80%、200px、30vw、5rem
        if (/^\d+(px|%|rem|em|vw)$/.test(overlayWidth)) {
            return overlayWidth;
        }
        // 带单位的相对值
        // 如: -100px 代表 100% - 100px。+10vw 代表 100% + 10vw
        if (/^(\+|\-)\d+(px|%|rem|em|vw)$/.test(overlayWidth)) {
            // 不能使用 calc(100% $1 $2)，需要考虑到 popOverContainer
            return overlayWidth.replace(/^(\+|\-)(.*)/, "calc(".concat(targetWidth, "px $1 $2)"));
        }
        return;
    };
    PopOverContainer.alignToPlacement = function (overlay) {
        var align = overlay && overlay.align;
        return (align && PopOverContainer.alignPlacementMap[align]) || 'auto';
    };
    // 可以自定义下拉框宽度
    PopOverContainer.prototype.getOverlayStyle = function () {
        var _a;
        var _b;
        var _c = this.props, overlayWidth = _c.overlayWidth, overlayWidthField = _c.overlayWidthField;
        return _a = {},
            _a[overlayWidthField || 'minWidth'] = PopOverContainer.calcOverlayWidth({ width: overlayWidth }, (_b = this.target) === null || _b === void 0 ? void 0 : _b.offsetWidth) || (this.target ? Math.max(this.target.offsetWidth, 100) : 'auto'),
            _a;
    };
    PopOverContainer.prototype.render = function () {
        var _a = this.props, mobileUI = _a.mobileUI, children = _a.children, popOverContainer = _a.popOverContainer, popOverClassName = _a.popOverClassName, dropdownRender = _a.popOverRender, placement = _a.placement, align = _a.align, showConfirm = _a.showConfirm; _a.onConfirm; var disabled = _a.disabled;
        return (_J$X_(React__default["default"].Fragment, null,
            children({
                isOpened: this.state.isOpened && this.props.show !== false,
                onClick: this.handleClick,
                ref: this.targetRef,
                disabled: disabled
            }),
            mobileUI ? (_J$X_(PopUp["default"], { isShow: this.state.isOpened && this.props.show !== false, container: popOverContainer, className: popOverClassName, showConfirm: showConfirm, onHide: this.close, onConfirm: this.onConfirm }, dropdownRender({ onClose: this.close }))) : (_J$X_(amisCore.Overlay, { container: popOverContainer || this.getParent, target: this.getTarget, placement: placement || PopOverContainer.alignToPlacement({ align: align }), show: this.state.isOpened && this.props.show !== false },
                _J$X_(amisCore.PopOver, { overlay: true, className: popOverClassName, style: this.getOverlayStyle(), onHide: this.close }, dropdownRender({ onClose: this.close }))))));
    };
    PopOverContainer.alignPlacementMap = {
        left: 'left-bottom-left-top',
        right: 'right-bottom-right-top',
        center: 'center-bottom-center-top'
    };
    tslib.__decorate([
        amisCore.autobind,
        tslib.__metadata("design:type", Function),
        tslib.__metadata("design:paramtypes", [Object]),
        tslib.__metadata("design:returntype", void 0)
    ], PopOverContainer.prototype, "targetRef", null);
    tslib.__decorate([
        amisCore.autobind,
        tslib.__metadata("design:type", Function),
        tslib.__metadata("design:paramtypes", [Object]),
        tslib.__metadata("design:returntype", void 0)
    ], PopOverContainer.prototype, "handleClick", null);
    tslib.__decorate([
        amisCore.autobind,
        tslib.__metadata("design:type", Function),
        tslib.__metadata("design:paramtypes", []),
        tslib.__metadata("design:returntype", void 0)
    ], PopOverContainer.prototype, "close", null);
    tslib.__decorate([
        amisCore.autobind,
        tslib.__metadata("design:type", Function),
        tslib.__metadata("design:paramtypes", []),
        tslib.__metadata("design:returntype", void 0)
    ], PopOverContainer.prototype, "getTarget", null);
    tslib.__decorate([
        amisCore.autobind,
        tslib.__metadata("design:type", Function),
        tslib.__metadata("design:paramtypes", []),
        tslib.__metadata("design:returntype", void 0)
    ], PopOverContainer.prototype, "getParent", null);
    tslib.__decorate([
        amisCore.autobind,
        tslib.__metadata("design:type", Function),
        tslib.__metadata("design:paramtypes", []),
        tslib.__metadata("design:returntype", void 0)
    ], PopOverContainer.prototype, "onConfirm", null);
    return PopOverContainer;
}(React__default["default"].Component));

exports.PopOverContainer = PopOverContainer;
exports["default"] = PopOverContainer;
