"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.pageByVisibility = void 0;
var layout_1 = require("../../../utils/layout");
var scroll_1 = require("../../../utils/scroll");
var tiles_1 = require("../../../utils/tiles");
var scrollSnapEnabled = function (container) {
    var _a;
    var snapRule = (_a = getComputedStyle(container).getPropertyValue('scroll-snap-type')) !== null && _a !== void 0 ? _a : 'none';
    return snapRule !== 'none';
};
var pageLeft = function (container) {
    var _a;
    var targetOffset = layout_1.getTileTargetZoneOffsets(container);
    var targetLeftEdge = layout_1.targetZoneLeftEdge(container, targetOffset);
    var tiles = tiles_1.getTiles(container);
    var leftTiles = tiles
        .filter(function (tile) { return layout_1.leftEdgeOffset(tile) < targetLeftEdge; });
    var nextTile = leftTiles[leftTiles.length - 1];
    if (nextTile == null) {
        scroll_1.scrollTo(container, 0, true);
    }
    else if (scrollSnapEnabled(container)) {
        var projectedLeftEdge_1 = layout_1.projectTargetZoneLeftEdge(layout_1.alignAtTargetZoneRightEdge(container, targetOffset, nextTile), targetOffset);
        var nextLeftTile = (_a = leftTiles.find(function (tile) { return layout_1.leftEdgeOffset(tile) >= projectedLeftEdge_1; })) !== null && _a !== void 0 ? _a : nextTile;
        scroll_1.scrollTo(container, layout_1.alignAtTargetZoneLeftEdge(container, targetOffset, nextLeftTile), true);
    }
    else {
        scroll_1.scrollTo(container, layout_1.alignAtTargetZoneRightEdge(container, targetOffset, nextTile), true);
    }
};
var pageRight = function (container) {
    var targetOffset = layout_1.getTileTargetZoneOffsets(container);
    var targetRightEdge = layout_1.targetZoneRightEdge(container, targetOffset);
    var tiles = tiles_1.getTiles(container);
    var rightTiles = tiles
        .filter(function (tile) { return layout_1.rightEdgeOffset(tile) > targetRightEdge; });
    var nextTile = rightTiles[0];
    if (nextTile == null) {
        scroll_1.scrollTo(container, container.scrollWidth - container.clientWidth, true);
    }
    else {
        scroll_1.scrollTo(container, layout_1.alignAtTargetZoneLeftEdge(container, targetOffset, nextTile), true);
    }
};
exports.pageByVisibility = { pageLeft: pageLeft, pageRight: pageRight };
