import { Arr, Num, Obj } from '@ephox/katamari';
import * as Boxes from '../../alien/Boxes';
const getRestriction = (anchor, restriction) => {
    switch (restriction) {
        case 1 /* LeftEdge */:
            return anchor.x;
        case 0 /* RightEdge */:
            return anchor.x + anchor.width;
        case 2 /* TopEdge */:
            return anchor.y;
        case 3 /* BottomEdge */:
            return anchor.y + anchor.height;
    }
};
export const boundsRestriction = (anchor, restrictions) => Arr.mapToObject(['left', 'right', 'top', 'bottom'], (dir) => Obj.get(restrictions, dir).map((restriction) => getRestriction(anchor, restriction)));
export const adjustBounds = (bounds, restriction, bubbleOffset) => {
    const applyRestriction = (dir, current) => restriction[dir].map((pos) => {
        const isVerticalAxis = dir === 'top' || dir === 'bottom';
        const offset = isVerticalAxis ? bubbleOffset.top : bubbleOffset.left;
        const comparator = dir === 'left' || dir === 'top' ? Math.max : Math.min;
        const newPos = comparator(pos, current) + offset;
        // Ensure the new restricted position is within the current bounds
        return isVerticalAxis ? Num.clamp(newPos, bounds.y, bounds.bottom) : Num.clamp(newPos, bounds.x, bounds.right);
    }).getOr(current);
    const adjustedLeft = applyRestriction('left', bounds.x);
    const adjustedTop = applyRestriction('top', bounds.y);
    const adjustedRight = applyRestriction('right', bounds.right);
    const adjustedBottom = applyRestriction('bottom', bounds.bottom);
    return Boxes.bounds(adjustedLeft, adjustedTop, adjustedRight - adjustedLeft, adjustedBottom - adjustedTop);
};
//# sourceMappingURL=LayoutBounds.js.map