/**
 * DevExtreme (cjs/__internal/ui/overlay/m_overlay_position_controller.js)
 * Version: 23.2.9 (build 24294-0112)
 * Build date: Sun Oct 20 2024
 *
 * Copyright (c) 2012 - 2024 Developer Express Inc. ALL RIGHTS RESERVED
 * Read about DevExtreme licensing here: https://js.devexpress.com/Licensing/
 */
"use strict";
Object.defineProperty(exports, "__esModule", {
    value: true
});
exports.OverlayPositionController = exports.OVERLAY_POSITION_ALIASES = void 0;
var _position = _interopRequireDefault(require("../../../animation/position"));
var _translator = require("../../../animation/translator");
var _renderer = _interopRequireDefault(require("../../../core/renderer"));
var _extend = require("../../../core/utils/extend");
var _type = require("../../../core/utils/type");
var _window = require("../../../core/utils/window");
var _swatch_container = _interopRequireDefault(require("../../../ui/widget/swatch_container"));

function _interopRequireDefault(obj) {
    return obj && obj.__esModule ? obj : {
        default: obj
    }
}

function _defineProperties(target, props) {
    for (var i = 0; i < props.length; i++) {
        var descriptor = props[i];
        descriptor.enumerable = descriptor.enumerable || false;
        descriptor.configurable = true;
        if ("value" in descriptor) {
            descriptor.writable = true
        }
        Object.defineProperty(target, _toPropertyKey(descriptor.key), descriptor)
    }
}

function _createClass(Constructor, protoProps, staticProps) {
    if (protoProps) {
        _defineProperties(Constructor.prototype, protoProps)
    }
    if (staticProps) {
        _defineProperties(Constructor, staticProps)
    }
    Object.defineProperty(Constructor, "prototype", {
        writable: false
    });
    return Constructor
}

function _toPropertyKey(arg) {
    var key = _toPrimitive(arg, "string");
    return "symbol" === typeof key ? key : String(key)
}

function _toPrimitive(input, hint) {
    if ("object" !== typeof input || null === input) {
        return input
    }
    var prim = input[Symbol.toPrimitive];
    if (void 0 !== prim) {
        var res = prim.call(input, hint || "default");
        if ("object" !== typeof res) {
            return res
        }
        throw new TypeError("@@toPrimitive must return a primitive value.")
    }
    return ("string" === hint ? String : Number)(input)
}
const window = (0, _window.getWindow)();
const OVERLAY_POSITION_ALIASES = {
    top: {
        my: "top center",
        at: "top center"
    },
    bottom: {
        my: "bottom center",
        at: "bottom center"
    },
    right: {
        my: "right center",
        at: "right center"
    },
    left: {
        my: "left center",
        at: "left center"
    },
    center: {
        my: "center",
        at: "center"
    },
    "right bottom": {
        my: "right bottom",
        at: "right bottom"
    },
    "right top": {
        my: "right top",
        at: "right top"
    },
    "left bottom": {
        my: "left bottom",
        at: "left bottom"
    },
    "left top": {
        my: "left top",
        at: "left top"
    }
};
exports.OVERLAY_POSITION_ALIASES = OVERLAY_POSITION_ALIASES;
const OVERLAY_DEFAULT_BOUNDARY_OFFSET = {
    h: 0,
    v: 0
};
let OverlayPositionController = function() {
    function OverlayPositionController(_ref) {
        let {
            position: position,
            container: container,
            visualContainer: visualContainer,
            $root: $root,
            $content: $content,
            $wrapper: $wrapper,
            onPositioned: onPositioned,
            onVisualPositionChanged: onVisualPositionChanged,
            restorePosition: restorePosition,
            _fixWrapperPosition: _fixWrapperPosition,
            _skipContentPositioning: _skipContentPositioning
        } = _ref;
        this._props = {
            position: position,
            container: container,
            visualContainer: visualContainer,
            restorePosition: restorePosition,
            onPositioned: onPositioned,
            onVisualPositionChanged: onVisualPositionChanged,
            _fixWrapperPosition: _fixWrapperPosition,
            _skipContentPositioning: _skipContentPositioning
        };
        this._$root = $root;
        this._$content = $content;
        this._$wrapper = $wrapper;
        this._$markupContainer = void 0;
        this._$visualContainer = void 0;
        this._shouldRenderContentInitialPosition = true;
        this._visualPosition = void 0;
        this._initialPosition = void 0;
        this._previousVisualPosition = void 0;
        this.updateContainer(container);
        this.updatePosition(position);
        this.updateVisualContainer(visualContainer)
    }
    var _proto = OverlayPositionController.prototype;
    _proto.restorePositionOnNextRender = function(value) {
        this._shouldRenderContentInitialPosition = value || !this._visualPosition
    };
    _proto.openingHandled = function() {
        const shouldRestorePosition = this._props.restorePosition;
        this.restorePositionOnNextRender(shouldRestorePosition)
    };
    _proto.updatePosition = function(positionProp) {
        this._props.position = positionProp;
        this._position = this._normalizePosition(positionProp);
        this.updateVisualContainer()
    };
    _proto.updateContainer = function() {
        let containerProp = arguments.length > 0 && void 0 !== arguments[0] ? arguments[0] : this._props.container;
        this._props.container = containerProp;
        this._$markupContainer = containerProp ? (0, _renderer.default)(containerProp) : _swatch_container.default.getSwatchContainer(this._$root);
        this.updateVisualContainer(this._props.visualContainer)
    };
    _proto.updateVisualContainer = function() {
        let visualContainer = arguments.length > 0 && void 0 !== arguments[0] ? arguments[0] : this._props.visualContainer;
        this._props.visualContainer = visualContainer;
        this._$visualContainer = this._getVisualContainer()
    };
    _proto.detectVisualPositionChange = function(event) {
        this._updateVisualPositionValue();
        this._raisePositionedEvents(event)
    };
    _proto.positionContent = function() {
        if (this._shouldRenderContentInitialPosition) {
            this._renderContentInitialPosition()
        } else {
            (0, _translator.move)(this._$content, this._visualPosition);
            this.detectVisualPositionChange()
        }
    };
    _proto.positionWrapper = function() {
        if (this._$visualContainer) {
            _position.default.setup(this._$wrapper, {
                my: "top left",
                at: "top left",
                of: this._$visualContainer
            })
        }
    };
    _proto.styleWrapperPosition = function() {
        const useFixed = (0, _type.isWindow)(this.$visualContainer.get(0)) || this._props._fixWrapperPosition;
        const positionStyle = useFixed ? "fixed" : "absolute";
        this._$wrapper.css("position", positionStyle)
    };
    _proto._updateVisualPositionValue = function() {
        this._previousVisualPosition = this._visualPosition;
        this._visualPosition = (0, _translator.locate)(this._$content)
    };
    _proto._renderContentInitialPosition = function() {
        this._renderBoundaryOffset();
        (0, _translator.resetPosition)(this._$content);
        const wrapperOverflow = this._$wrapper.css("overflow");
        this._$wrapper.css("overflow", "hidden");
        if (!this._props._skipContentPositioning) {
            const resultPosition = _position.default.setup(this._$content, this._position);
            this._initialPosition = resultPosition
        }
        this._$wrapper.css("overflow", wrapperOverflow);
        this.detectVisualPositionChange()
    };
    _proto._raisePositionedEvents = function(event) {
        const previousPosition = this._previousVisualPosition;
        const newPosition = this._visualPosition;
        const isVisualPositionChanged = (null === previousPosition || void 0 === previousPosition ? void 0 : previousPosition.top) !== newPosition.top || (null === previousPosition || void 0 === previousPosition ? void 0 : previousPosition.left) !== newPosition.left;
        if (isVisualPositionChanged) {
            this._props.onVisualPositionChanged({
                previousPosition: previousPosition,
                position: newPosition,
                event: event
            })
        }
        this._props.onPositioned({
            position: this._initialPosition
        })
    };
    _proto._renderBoundaryOffset = function() {
        var _a;
        const boundaryOffset = null !== (_a = this._position) && void 0 !== _a ? _a : {
            boundaryOffset: OVERLAY_DEFAULT_BOUNDARY_OFFSET
        };
        this._$content.css("margin", "".concat(boundaryOffset.v, "px ").concat(boundaryOffset.h, "px"))
    };
    _proto._getVisualContainer = function() {
        var _a, _b;
        const containerProp = this._props.container;
        const visualContainerProp = this._props.visualContainer;
        const positionOf = (0, _type.isEvent)(null === (_a = this._props.position) || void 0 === _a ? void 0 : _a.of) ? this._props.position.of.target : null === (_b = this._props.position) || void 0 === _b ? void 0 : _b.of;
        if (visualContainerProp) {
            return (0, _renderer.default)(visualContainerProp)
        }
        if (containerProp) {
            return (0, _renderer.default)(containerProp)
        }
        if (positionOf) {
            return (0, _renderer.default)(positionOf)
        }
        return (0, _renderer.default)(window)
    };
    _proto._normalizePosition = function(positionProp) {
        const defaultPositionConfig = {
            boundaryOffset: OVERLAY_DEFAULT_BOUNDARY_OFFSET
        };
        if ((0, _type.isDefined)(positionProp)) {
            return (0, _extend.extend)(true, {}, defaultPositionConfig, this._positionToObject(positionProp))
        }
        return defaultPositionConfig
    };
    _proto._positionToObject = function(position) {
        if ((0, _type.isString)(position)) {
            return (0, _extend.extend)({}, OVERLAY_POSITION_ALIASES[position])
        }
        return position
    };
    _createClass(OverlayPositionController, [{
        key: "$container",
        get: function() {
            this.updateContainer();
            return this._$markupContainer
        }
    }, {
        key: "$visualContainer",
        get: function() {
            return this._$visualContainer
        }
    }, {
        key: "position",
        get: function() {
            return this._position
        }
    }, {
        key: "fixWrapperPosition",
        set: function(fixWrapperPosition) {
            this._props._fixWrapperPosition = fixWrapperPosition;
            this.styleWrapperPosition()
        }
    }, {
        key: "restorePosition",
        set: function(restorePosition) {
            this._props.restorePosition = restorePosition
        }
    }]);
    return OverlayPositionController
}();
exports.OverlayPositionController = OverlayPositionController;
