"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.CommentsReducer = exports.GetCellCommentSelector = exports.CommentsDelete = exports.CommentsEdit = exports.CommentsCellDelete = exports.CommentsCellAdd = exports.CommentsAdd = exports.CommentsSet = exports.COMMENTS_LOAD = exports.COMMENTS_READY = exports.COMMENTS_DELETE = exports.COMMENTS_EDIT = exports.COMMENTS_CELL_DELETE = exports.COMMENTS_CELL_ADD = exports.COMMENTS_ADD = void 0;
const tslib_1 = require("tslib");
const GeneralConstants_1 = require("../../Utilities/Constants/GeneralConstants");
const ObjectFactory_1 = tslib_1.__importDefault(require("../../Utilities/ObjectFactory"));
const CellPopupService_1 = require("../../Utilities/Services/CellPopupService");
/**
 * @ReduxAction A Comments has been added
 */
exports.COMMENTS_ADD = 'COMMENTS_ADD';
/**
 * @ReduxAction Cell Comments has been added
 */
exports.COMMENTS_CELL_ADD = 'COMMENTS_CELL_ADD';
/**
 * @ReduxAction Cell comments have been removed
 */
exports.COMMENTS_CELL_DELETE = 'COMMENTS_CELL_DELETE';
/**
 * @ReduxAction A Comments has been edited
 */
exports.COMMENTS_EDIT = 'COMMENTS_EDIT';
/**
 * @ReduxAction A Comments has been deleted
 */
exports.COMMENTS_DELETE = 'COMMENTS_DELETE';
/**
 * @ReduxAction Comments Module is ready
 */
exports.COMMENTS_READY = 'COMMENTS_READY';
exports.COMMENTS_LOAD = 'COMMENTS_LOAD';
const CommentsSet = ({ commentThread, }) => ({
    type: exports.COMMENTS_LOAD,
    commentThread,
});
exports.CommentsSet = CommentsSet;
const CommentsAdd = ({ comment, cellAddress, }) => ({
    type: exports.COMMENTS_ADD,
    comment,
    cellAddress,
});
exports.CommentsAdd = CommentsAdd;
const CommentsCellAdd = ({ commentThread, }) => ({
    type: exports.COMMENTS_CELL_ADD,
    commentThread,
});
exports.CommentsCellAdd = CommentsCellAdd;
const CommentsCellDelete = ({ cellAddress: cellAddress, }) => ({
    type: exports.COMMENTS_CELL_DELETE,
    cellAddress: cellAddress,
});
exports.CommentsCellDelete = CommentsCellDelete;
const CommentsEdit = ({ comment, cellAddress, }) => ({
    type: exports.COMMENTS_EDIT,
    comment,
    cellAddress,
});
exports.CommentsEdit = CommentsEdit;
const CommentsDelete = ({ commentUuid, cellAddress, }) => ({
    type: exports.COMMENTS_DELETE,
    cellAddress,
    commentUuid,
});
exports.CommentsDelete = CommentsDelete;
const GetCellCommentSelector = (state, address) => {
    var _a;
    if (!address) {
        return undefined;
    }
    return ((_a = state === null || state === void 0 ? void 0 : state.CommentThreads) !== null && _a !== void 0 ? _a : []).find((commentThread) => {
        return CellPopupService_1.CellPopupService.isSameAddress(commentThread, address);
    });
};
exports.GetCellCommentSelector = GetCellCommentSelector;
const initialState = {
    CommentThreads: GeneralConstants_1.EMPTY_ARRAY,
};
const CommentsReducer = (state = initialState, action) => {
    var _a, _b, _c, _d, _e;
    switch (action.type) {
        case exports.COMMENTS_ADD: {
            const addAction = action;
            const commentThread = (_a = state.CommentThreads) !== null && _a !== void 0 ? _a : [];
            const cellCommentIndex = commentThread.findIndex((cellComments) => {
                return CellPopupService_1.CellPopupService.isSameAddress(cellComments, addAction.cellAddress);
            });
            let comments = [...commentThread];
            if (cellCommentIndex === -1) {
                // new comments
                comments.push(Object.assign(Object.assign({}, ObjectFactory_1.default.CreateEmptyCommentThread(addAction.cellAddress)), { Comments: [addAction.comment] }));
            }
            else {
                const existing = Object.assign(Object.assign({}, commentThread[cellCommentIndex]), { Comments: [...commentThread[cellCommentIndex].Comments, addAction.comment] });
                comments[cellCommentIndex] = existing;
            }
            return Object.assign(Object.assign({}, state), { CommentThreads: comments });
        }
        case exports.COMMENTS_CELL_ADD: {
            return Object.assign(Object.assign({}, state), { CommentThreads: [
                    ...((_b = state.CommentThreads) !== null && _b !== void 0 ? _b : []),
                    action.commentThread,
                ] });
        }
        case exports.COMMENTS_CELL_DELETE: {
            const deleteAction = action;
            return Object.assign(Object.assign({}, state), { CommentThreads: ((_c = state.CommentThreads) !== null && _c !== void 0 ? _c : []).filter((cellComments) => !CellPopupService_1.CellPopupService.isSameAddress(cellComments, deleteAction.cellAddress)) });
        }
        case exports.COMMENTS_EDIT: {
            const editAction = action;
            let edited = false;
            const adaptableCellComments = ((_d = state.CommentThreads) !== null && _d !== void 0 ? _d : []).map((cellComments) => {
                if (CellPopupService_1.CellPopupService.isSameAddress(cellComments, editAction.cellAddress)) {
                    // found the place
                    edited = true;
                    return Object.assign(Object.assign({}, cellComments), { Comments: cellComments.Comments.map((comment) => {
                            if (comment.Uuid === editAction.comment.Uuid) {
                                return editAction.comment;
                            }
                            return comment;
                        }) });
                }
            });
            return Object.assign(Object.assign({}, state), { CommentThreads: adaptableCellComments });
        }
        case exports.COMMENTS_DELETE: {
            const deleteAction = action;
            const adaptableCellComments = ((_e = state.CommentThreads) !== null && _e !== void 0 ? _e : [])
                .map((cellComments) => {
                if (CellPopupService_1.CellPopupService.isSameAddress(cellComments, deleteAction.cellAddress)) {
                    // found the place
                    const newComments = cellComments.Comments.filter((comment) => comment.Uuid !== deleteAction.commentUuid);
                    if (newComments.length === 0) {
                        return null;
                    }
                    const newCellComments = Object.assign(Object.assign({}, cellComments), { Comments: newComments });
                    return newCellComments;
                }
                return cellComments;
            })
                .filter((cellComments) => cellComments !== null);
            return Object.assign(Object.assign({}, state), { CommentThreads: adaptableCellComments });
        }
        case exports.COMMENTS_LOAD: {
            return Object.assign(Object.assign({}, state), { CommentThreads: action.commentThread });
        }
        default:
            return state;
    }
};
exports.CommentsReducer = CommentsReducer;
