"use strict";
var __importDefault = (this && this.__importDefault) || function (mod) {
    return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.BasketReducer = void 0;
const events_1 = require("../events/index.js");
const dayjs_1 = __importDefault(require("dayjs"));
const RemoveFromCartEvent_1 = require("../events/actions/RemoveFromCartEvent");
const RoomstayEventManager_1 = __importDefault(require("../events/RoomstayEventManager"));
const BasketReducer = (rows, action) => {
    var _a;
    const checkAndSetRowDates = (row) => {
        if (!row.getStartDate()) {
            row.setStartDate((0, dayjs_1.default)('now'));
        }
        if (!row.getEndDate()) {
            row.setEndDate((0, dayjs_1.default)('now').add(1, 'day'));
        }
    };
    switch (action.method) {
        case 'Add':
            if (action.row) {
                const { row } = action;
                checkAndSetRowDates(row);
                return [...rows, row];
            }
            return rows;
        case 'AddRate':
            if (action.row && action.rate) {
                const { row } = action;
                checkAndSetRowDates(row);
                row.setRate(action.rate);
                let hasRowIncluded = false;
                // Let us loop through all of our rows and see if it's in array
                // if target row isn't included, we'll add it to the end.
                const newRows = rows.map((mappedRow) => {
                    var _a;
                    if (mappedRow.getID() === ((_a = action.row) === null || _a === void 0 ? void 0 : _a.getID())) {
                        hasRowIncluded = true;
                        return action.row;
                    }
                    else {
                        return mappedRow;
                    }
                });
                if (!hasRowIncluded) {
                    newRows.push(row);
                }
                // Reason we raise this event here, and not in the reducer for example is
                // this is the only sure way we can trust a room is being added to the cart
                const addToCartEvent = new events_1.AddToCartEvent(row, newRows);
                RoomstayEventManager_1.default.raise(addToCartEvent);
                return [...newRows];
            }
            return rows;
        case 'Remove':
            if (action.row) {
                const newRows = rows.filter((item) => { var _a; return item.getID() !== ((_a = action.row) === null || _a === void 0 ? void 0 : _a.getID()); });
                const removeFromCartEvent = new RemoveFromCartEvent_1.RemoveFromCartEvent(action.row, newRows);
                RoomstayEventManager_1.default.raise(removeFromCartEvent);
                return newRows;
            }
            return rows;
        case 'Clear':
            return [];
        case 'Update':
            if (action.row) {
                return rows.map((row) => {
                    if (row.getID() === action.row.getID()) {
                        return action.row;
                    }
                    else {
                        return row;
                    }
                });
            }
            return rows;
        case 'Init': {
            if ((_a = action.rows) === null || _a === void 0 ? void 0 : _a.length) {
                action.rows.forEach((row) => checkAndSetRowDates(row));
            }
            return action.rows || [];
        }
    }
};
exports.BasketReducer = BasketReducer;
//# sourceMappingURL=BasketReducer.js.map