import Popper from 'popper.js';
import { merge } from '../../internal';
import { cssClass } from '../../styles';
export function dialogContext(ctx, level, attachment) {
    return {
        attachment: attachment.currentTarget != null ? attachment.currentTarget : attachment,
        level: level,
        manager: ctx.dialogManager,
        idPrefix: ctx.idPrefix
    };
}
var ADialog = /** @class */ (function () {
    function ADialog(dialog, options) {
        if (options === void 0) { options = {}; }
        this.dialog = dialog;
        this.options = {
            title: '',
            fullDialog: false,
            resetPossible: true,
            placement: 'bottom-start',
            toggleDialog: true,
            modifiers: {}
        };
        this.popper = null;
        Object.assign(this.options, options);
        this.node = dialog.attachment.ownerDocument.createElement('form');
        this.node.classList.add(cssClass('dialog'));
    }
    Object.defineProperty(ADialog.prototype, "attachment", {
        get: function () {
            return this.dialog.attachment;
        },
        enumerable: true,
        configurable: true
    });
    Object.defineProperty(ADialog.prototype, "level", {
        get: function () {
            return this.dialog.level;
        },
        enumerable: true,
        configurable: true
    });
    ADialog.prototype.equals = function (that) {
        return this.dialog.level === that.dialog.level && this.dialog.attachment === that.dialog.attachment;
    };
    ADialog.prototype.appendDialogButtons = function () {
        this.node.insertAdjacentHTML('beforeend', "<div class=\"" + cssClass('dialog-buttons') + "\">\n      <button class=\"" + cssClass('dialog-button') + "\" type=\"submit\" title=\"Apply\"></button>\n      <button class=\"" + cssClass('dialog-button') + "\" type=\"button\" title=\"Cancel\"></button>\n      <button class=\"" + cssClass('dialog-button') + "\" type=\"reset\" title=\"Reset to default values\" " + (!this.options.resetPossible ? 'style="visibility: hidden"' : '') + "></button>\n    </div>");
    };
    ADialog.prototype.open = function () {
        var _this = this;
        if (this.options.toggleDialog && this.dialog.manager.removeLike(this)) {
            return;
        }
        if (this.build(this.node) === false) {
            return;
        }
        var parent = this.attachment.closest("." + cssClass());
        if (this.options.title) {
            this.node.insertAdjacentHTML('afterbegin', "<strong>" + this.options.title + "</strong>");
        }
        if (this.options.fullDialog) {
            this.appendDialogButtons();
        }
        parent.appendChild(this.node);
        this.popper = new Popper(this.attachment, this.node, merge({
            modifiers: {
                preventOverflow: {
                    boundariesElement: parent
                }
            }
        }, this.options));
        var auto = this.find('input[autofocus]');
        if (auto) {
            // delay such that it works
            self.setTimeout(function () { return auto.focus(); });
        }
        var reset = this.find('button[type=reset]');
        if (reset) {
            reset.onclick = function (evt) {
                evt.stopPropagation();
                evt.preventDefault();
                _this.reset();
            };
        }
        this.node.onsubmit = function (evt) {
            evt.stopPropagation();
            evt.preventDefault();
            if (!_this.node.checkValidity()) {
                return false;
            }
            if (_this.submit()) {
                _this.destroy();
            }
            return false;
        };
        var cancel = this.find('button[title=Cancel]');
        if (cancel) {
            cancel.onclick = function (evt) {
                evt.stopPropagation();
                evt.preventDefault();
                _this.destroy();
            };
        }
        this.dialog.manager.push(this);
    };
    ADialog.prototype.find = function (selector) {
        return this.node.querySelector(selector);
    };
    ADialog.prototype.findInput = function (selector) {
        return this.find(selector);
    };
    ADialog.prototype.forEach = function (selector, callback) {
        return Array.from(this.node.querySelectorAll(selector)).map(callback);
    };
    ADialog.prototype.reset = function () {
        // hook
    };
    ADialog.prototype.submit = function () {
        // hook
        return true;
    };
    ADialog.prototype.destroy = function () {
        this.dialog.manager.remove(this);
        if (this.popper) {
            this.popper.destroy();
        }
        this.node.remove();
    };
    return ADialog;
}());
export default ADialog;
//# sourceMappingURL=ADialog.js.map