"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
var Renderables_1 = require("../Renderables");
var node_1 = require("../../support/node");
var Specification_1 = require("../../support/Specification");
var MessageType_1 = require("../../support/MessageType");
var EventType_1 = require("../../event/EventType");
var EventStream_1 = require("../../event/EventStream");
/**
 * Implementation of the 'rating' markup element. A div HTML element
 * is created and the class lto-rating is added to allow CSS modifications.
 *
 * @see {@link IRenderable}
 * @see {@link IStackeable}
 */
var Rating = /** @class */ (function () {
    function Rating(message) {
        this.spec = message;
        this.ratingContainer = node_1.default("div");
        this.ratingButtons = new Map();
        this.selectedRatingButtonType = RatingButtonType.NOT_YET_SELECTED;
    }
    /**
     * @inheritDoc
     */
    Rating.prototype.render = function (renderer, isNested) {
        // Div for both buttons
        new Specification_1.Specification(this.spec).initNode(this.ratingContainer, "lto-rating");
        this.ratingContainer.addAttributes({ type: Rating.TYPE });
        this.renderRatingButton();
        return this.ratingContainer.unwrap();
    };
    Rating.prototype.renderRatingButton = function () {
        var _this = this;
        // Like and dislike buttons
        var buttonLike = node_1.default("button").addClasses("lto-button");
        var buttonDislike = node_1.default("button").addClasses("lto-button");
        buttonLike.innerText('👍');
        this.ratingContainer.appendChild(buttonLike);
        buttonDislike.innerText('👎');
        this.ratingContainer.appendChild(buttonDislike);
        this.ratingButtons.set(RatingButtonType.LIKE, {
            type: RatingButtonType.LIKE,
            buttonNode: buttonLike,
            score: 1
        });
        this.ratingButtons.set(RatingButtonType.DISLIKE, {
            type: RatingButtonType.DISLIKE,
            buttonNode: buttonDislike,
            score: 0
        });
        // Comment form to be appended after rating button is clicked
        var commentForm = node_1.default("form").addClasses("lto-form");
        commentForm.appendChild(node_1.default("input").addAttributes({ type: "text" }).addAttributes({ placeholder: "optional" }));
        var submitButton = node_1.default("button").innerText("Send rating").addClasses("lto-button");
        commentForm.appendChild(submitButton);
        // Click events
        buttonLike.onClick(function (ev) {
            ev.preventDefault();
            _this.ratingButtonOnClick(RatingButtonType.LIKE, commentForm);
        });
        buttonDislike.onClick(function (ev) {
            ev.preventDefault();
            _this.ratingButtonOnClick(RatingButtonType.DISLIKE, commentForm);
        });
        submitButton.onClick(function (ev) {
            ev.preventDefault();
            var score = _this.getScoreOfClickedButton();
            var payload = { score: score };
            var comment = commentForm.unwrap().firstChild.value;
            var attributes = { comment: comment };
            var type = MessageType_1.MessageType.RATING;
            var evType = EventType_1.EventType.withChannelId(EventType_1.EventType.PUBLISH, _this.spec.channelId);
            EventStream_1.default.emit(evType, { attributes: attributes, type: type, payload: payload });
            // Possibility to hide the rating container once it has been submitted
            _this.ratingContainer.addClasses("lto-rating-submitted");
        });
    };
    Rating.prototype.ratingButtonOnClick = function (buttonType, commentForm) {
        this.ratingContainer.appendChild(commentForm);
        this.selectedRatingButtonType = buttonType;
        // The button that was clicked
        this.ratingButtons.get(buttonType).buttonNode.addClasses("lto-button-last-clicked");
        // The other button
        this.ratingButtons.get((buttonType + 1) % 2).buttonNode.removeClasses("lto-button-last-clicked");
    };
    Rating.prototype.getScoreOfClickedButton = function () {
        return this.ratingButtons.get(this.selectedRatingButtonType).score.toString();
    };
    Rating.TYPE = "rating";
    return Rating;
}());
exports.Rating = Rating;
Renderables_1.default.register("rating", Rating);
var RatingButtonType;
(function (RatingButtonType) {
    RatingButtonType[RatingButtonType["LIKE"] = 0] = "LIKE";
    RatingButtonType[RatingButtonType["DISLIKE"] = 1] = "DISLIKE";
    RatingButtonType[RatingButtonType["NOT_YET_SELECTED"] = 2] = "NOT_YET_SELECTED";
})(RatingButtonType || (RatingButtonType = {}));
//# sourceMappingURL=index.js.map