import { __awaiter, __generator } from "tslib";
import { Component, triggerEvent, getValueFromProps } from '../_util/simply';
import { PopoverDefaultProps } from './props';
import mixinValue from '../mixins/value';
import { getInstanceBoundingClientRect } from '../_util/jsapi/get-instance-bounding-client-rect';
import { getSystemInfo } from '../_util/jsapi/get-system-info';
import { getPopoverStyle } from './utils';
Component(PopoverDefaultProps, {
    getInstance: function () {
        if (this.$id) {
            return my;
        }
        return this;
    },
    updatePopover: function () {
        return __awaiter(this, void 0, void 0, function () {
            var _a, placement, autoAdjustOverflow, _b, containerRect, childrenRect, contentRect, systemInfo, _c, popoverContentStyle, adjustedPlacement;
            return __generator(this, function (_d) {
                switch (_d.label) {
                    case 0:
                        _a = getValueFromProps(this, [
                            'placement',
                            'autoAdjustOverflow',
                        ]), placement = _a[0], autoAdjustOverflow = _a[1];
                        return [4 /*yield*/, Promise.all([
                                getInstanceBoundingClientRect(this.getInstance(), "#ant-popover-children".concat(this.$id ? "-".concat(this.$id) : '')),
                                getInstanceBoundingClientRect(this.getInstance(), this.$id
                                    ? "#ant-popover-children-".concat(this.$id, " > *")
                                    : "#ant-popover-children-container"),
                                getInstanceBoundingClientRect(this.getInstance(), this.$id
                                    ? "#ant-popover-content-".concat(this.$id)
                                    : '#ant-popover-content'),
                                getSystemInfo(),
                            ])];
                    case 1:
                        _b = _d.sent(), containerRect = _b[0], childrenRect = _b[1], contentRect = _b[2], systemInfo = _b[3];
                        _c = getPopoverStyle(placement, autoAdjustOverflow, {
                            containerRect: containerRect,
                            childrenRect: childrenRect,
                            contentRect: contentRect,
                            systemInfo: systemInfo,
                        }), popoverContentStyle = _c.popoverContentStyle, adjustedPlacement = _c.adjustedPlacement;
                        this.setData({
                            popoverContentStyle: popoverContentStyle,
                            adjustedPlacement: adjustedPlacement,
                        });
                        return [2 /*return*/];
                }
            });
        });
    },
    onVisibleChange: function (e) {
        if (!this.getValue() &&
            e.target.id &&
            e.target.id.indexOf('ant-popover-') === 0) {
            return;
        }
        var value = !this.getValue();
        if (!this.isControlled()) {
            this.update(value);
        }
        triggerEvent(this, 'visibleChange', value, e);
    },
}, {
    adjustedPlacement: '',
    popoverContentStyle: '',
}, [
    mixinValue({
        valueKey: 'visible',
        defaultValueKey: 'defaultVisible',
        transformValue: function (value) {
            if (value) {
                this.updatePopover();
            }
            else {
                this.setData({
                    adjustedPlacement: '',
                });
            }
            return {
                needUpdate: true,
                value: value,
            };
        },
    }),
], {
    didUpdate: function (prevProps) {
        var _a = getValueFromProps(this, [
            'placement',
            'autoAdjustOverflow',
        ]), placement = _a[0], autoAdjustOverflow = _a[1];
        if ((prevProps.placement !== placement ||
            prevProps.autoAdjustOverflow !== autoAdjustOverflow) &&
            this.getValue()) {
            this.updatePopover();
        }
    },
});
