"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
var EventStream_1 = require("../../event/EventStream");
var Renderables_1 = require("../Renderables");
var Elements_1 = require("../../support/Elements");
var button_1 = require("../button");
var InputContainer_1 = require("../../support/InputContainer");
var Overlay_1 = require("../overlays/Overlay");
var node_1 = require("../../support/node");
var EventType_1 = require("../../event/EventType");
var MessageType_1 = require("../../support/MessageType");
/**
 * Implementation of the 'submit' markup element.
 * A button HTML element is created of type submit and has to be
 * part of a form element in the markup. The class
 * lto-submit is added to allow CSS manipulations.
 *
 * @see {@link IRenderable}
 * @see {@link Form}
 */
var Submit = /** @class */ (function () {
    function Submit(spec) {
        this.spec = spec;
    }
    /**
     * @inheritDoc
     */
    Submit.prototype.render = function (renderer, isNested) {
        var _this = this;
        var position = this.spec.position || 'left';
        var submit = document.createElement('button');
        submit.setAttribute("type", Submit.TYPE);
        submit.classList.add("lto-submit", "lto-" + position);
        if (this.spec.id !== undefined) {
            submit.id = this.spec.id;
        }
        if (this.spec.class !== undefined) {
            this.spec.class.split(" ").forEach(function (e) { return submit.classList.add(e); });
        }
        if (isNested) {
            submit.classList.add("lto-nested");
        }
        submit.appendChild(document.createTextNode(this.spec.text || ""));
        var text = this.spec.text || "";
        submit.addEventListener("click", function (ev) {
            ev.preventDefault();
            var overlay = Elements_1.closestByClass(submit, ["lto-overlay"]);
            var container = Elements_1.closestByClass(submit, ["lto-container"]);
            if (overlay && container) {
                _this.handleOverlay(container, overlay);
                return;
            }
            var content = Elements_1.closestByClass(submit, ["lto-form"]);
            submit.disabled = true;
            if (content)
                content.style.pointerEvents = "none";
            InputContainer_1.InputContainer.getAll(content, submit).then(function (attr) {
                var evType = EventType_1.EventType.withChannelId(EventType_1.EventType.PUBLISH, _this.spec.channelId);
                var value = JSON.stringify(attr);
                var name = _this.spec.name;
                var type = MessageType_1.MessageType.SUBMIT;
                var payload = { value: value, text: text };
                var attributes = { name: name, value: value };
                EventStream_1.default.emit(evType, { type: type, payload: payload, attributes: attributes });
                button_1.Button.cleanupButtons(_this.spec.interactionContentClassName);
            }).catch(function (reason) {
                console.error("Unable to collect form input data: " + reason);
                submit.disabled = false;
                if (content)
                    content.style.pointerEvents = "auto";
            });
        });
        return submit;
    };
    Submit.prototype.handleOverlay = function (container, overlay) {
        var form = overlay.querySelector(".lto-form");
        var submit = overlay.querySelector(".lto-submit");
        var trigger = container.querySelector(".lto-trigger[name=\"" + overlay.getAttribute("name") + "\"]");
        if (!trigger || !form)
            return;
        InputContainer_1.InputContainer.getAll(form, submit).then(function (attr) {
            if (Object.keys(attr).length !== 0 && !trigger.classList.contains("lto-success")) {
                trigger.classList.add("lto-success");
                trigger.setAttribute("data-value", JSON.stringify(attr));
            }
            else if (Object.keys(attr).length === 0) {
                trigger.classList.remove("lto-success");
                if (trigger.hasAttribute("value")) {
                    trigger.removeAttribute("value");
                }
                else if (trigger.hasAttribute("data-value")) {
                    trigger.removeAttribute("data-value");
                }
            }
            Overlay_1.Overlay.hide(node_1.default(overlay));
        }).catch(function (reason) {
            console.error("Unable to collect form input data: " + reason);
            trigger.classList.remove("lto-success");
        });
    };
    Submit.TYPE = "submit";
    return Submit;
}());
exports.Submit = Submit;
Renderables_1.default.register(Submit.TYPE, Submit);
//# sourceMappingURL=index.js.map