<commit_before>import {BaseStore} from 'fluxible/addons';

class ContentDiscussionStore extends BaseStore {
    constructor(dispatcher) {
        super(dispatcher);
        this.discussion = [];
        this.selector = {};
        this.commentWithReplyBox = null;
    }
    static clearReplyFlags(comment) {
        comment.replyBoxOpened = false;
        if (comment.replies) comment.replies.map((reply) => {
            ContentDiscussionStore.clearReplyFlags(reply);
        });
    }
    updateDiscussion(payload) {
        payload.discussion.map((comment) => {
            ContentDiscussionStore.clearReplyFlags(comment);
        });
        this.commentWithReplyBox = null;
        this.discussion = payload.discussion;
        this.selector = payload.selector;
        this.emitChange();
    }
    addComment(payload) {
        this.discussion.push(payload.comment);
        this.emitChange();
    }
    findComment(array, identifier) {
        for(let i = 0; i < array.length; i++) {
            let comment = array[i];
            if (comment.id === identifier) {
                return comment;
            } else if (comment.replies !== undefined) {
                return (this.findComment(comment.replies, identifier));
            }
        };

        return null;
    }
    addReply(payload) {
        let parentComment = this.findComment(this.discussion, payload.parent_comment);
        if (parentComment.replies === undefined) {
            parentComment.replies = [];
        }
        parentComment.replies.push(payload);

        //close reply box
        if (this.commentWithReplyBox) this.commentWithReplyBox.replyBoxOpened = false;
        this.commentWithReplyBox = null;

        this.emitChange();
    }
    invertReplyBoxFlag(payload) {
        let comment = payload.comment;
        if (comment.replyBoxOpened) {
            comment.replyBoxOpened = false;
            this.commentWithReplyBox = null;
        }
        else {
            comment.replyBoxOpened = true;
            if (this.commentWithReplyBox) this.commentWithReplyBox.replyBoxOpened = false;
            this.commentWithReplyBox = comment;
        }
        this.emitChange();
    }
    getState() {
        return {
            discussion: this.discussion,
            selector: this.selector,
            commentWithReplyBox: this.commentWithReplyBox<commit_msg>SWIK-146 Corrected error related to parent comment search<commit_after>import {BaseStore} from 'fluxible/addons';

class ContentDiscussionStore extends BaseStore {
    constructor(dispatcher) {
        super(dispatcher);
        this.discussion = [];
        this.selector = {};
        this.commentWithReplyBox = null;
    }
    static clearReplyFlags(comment) {
        comment.replyBoxOpened = false;
        if (comment.replies) comment.replies.map((reply) => {
            ContentDiscussionStore.clearReplyFlags(reply);
        });
    }
    updateDiscussion(payload) {
        payload.discussion.map((comment) => {
            ContentDiscussionStore.clearReplyFlags(comment);
        });
        this.commentWithReplyBox = null;
        this.discussion = payload.discussion;
        this.selector = payload.selector;
        this.emitChange();
    }
    addComment(payload) {
        this.discussion.push(payload.comment);
        this.emitChange();
    }

    //Find comment in a tree with id = identifier
    findComment(array, identifier) {
        for(let i = 0; i < array.length; i++) {
            let comment = array[i];
            if (comment.id === identifier) {
                return comment;
            } else if (comment.replies !== undefined) {
              let commentFound = this.findComment(comment.replies, identifier);
              if (commentFound !== null)
                return commentFound;
            }
        }

        return null;
    }
    addReply(payload) {
        let parentComment = this.findComment(this.discussion, payload.parent_comment);
        if (parentComment !== null) {//found parent comment
            if (parentComment.replies === undefined) {//first reply
                parentComment.replies = [];
            }
            parentComment.replies.push(payload);
        }

        //close reply box
        if (this.commentWithReplyBox) this.commentWithReplyBox.replyBoxOpened = false;
        this.commentWithReplyBox = null;

        this.emitChange();
    }
    invertReplyBoxFlag(payload) {
        let comment = payload.comment;
        if (comment.replyBoxOpened) {
            comment.replyBoxOpened = false;
            this.commentWithReplyBox = null;
        }
        else {
            comment.replyBoxOpened = true;
            if (this.commentWithReplyBox) this.commentWithReplyBox.replyBoxOpened = false;
            this.commentWithReplyBox = comment;
        }
        this.emitChange();
    }
    getState() {
        return {
            discussion: this.discussion,
            selector: this.selector,
            commentWithReplyBox: this.commentWithReplyBox
{'fraction_non_alphanumeric': 0.10173895662602439, 'fraction_numerical': 0.0009994003597841295, 'mean_word_length': 2.0699386503067485, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 7, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '950859', 'n_tokens_mistral': 1331, 'n_tokens_neox': 1302, 'n_words': 244}