"use strict";
var __extends = (this && this.__extends) || function (d, b) {
    for (var p in b) if (b.hasOwnProperty(p)) d[p] = b[p];
    function __() { this.constructor = d; }
    d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
};
var __assign = (this && this.__assign) || Object.assign || function(t) {
    for (var s, i = 1, n = arguments.length; i < n; i++) {
        s = arguments[i];
        for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p))
            t[p] = s[p];
    }
    return t;
};
var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
    var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
    if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
    else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
    return c > 3 && r && Object.defineProperty(target, key, r), r;
};
var React = require("react");
var Utilities_1 = require("../../Utilities");
var index_1 = require("../FocusTrapZone/index");
var Dialog_Props_1 = require("./Dialog.Props");
var Overlay_1 = require("../../Overlay");
var Layer_1 = require("../../Layer");
var Button_1 = require("../../Button");
var DialogFooter_1 = require("./DialogFooter");
var index_2 = require("../Popup/index");
var withResponsiveMode_1 = require("../../utilities/decorators/withResponsiveMode");
var Dialog_scss_1 = require("./Dialog.scss");
var Dialog = (function (_super) {
    __extends(Dialog, _super);
    function Dialog(props) {
        var _this = _super.call(this, props) || this;
        _this._onDialogRef = _this._onDialogRef.bind(_this);
        _this.state = {
            id: Utilities_1.getId('Dialog'),
            isOpen: props.isOpen,
            isAnimatingOpen: props.isOpen,
            isAnimatingClose: false
        };
        return _this;
    }
    Dialog.prototype.componentWillReceiveProps = function (newProps) {
        // Opening the dialog
        if (newProps.isOpen && !this.state.isOpen) {
            this.setState({
                isOpen: true,
                isAnimatingOpen: true,
                isAnimatingClose: false
            });
        }
        // Closing the dialog
        if (!newProps.isOpen && this.state.isOpen) {
            this.setState({
                isAnimatingOpen: false,
                isAnimatingClose: true
            });
        }
    };
    Dialog.prototype.render = function () {
        var _a = this.props, closeButtonAriaLabel = _a.closeButtonAriaLabel, elementToFocusOnDismiss = _a.elementToFocusOnDismiss, firstFocusableSelector = _a.firstFocusableSelector, forceFocusInsideTrap = _a.forceFocusInsideTrap, ignoreExternalFocusing = _a.ignoreExternalFocusing, isBlocking = _a.isBlocking, isClickableOutsideFocusTrap = _a.isClickableOutsideFocusTrap, isDarkOverlay = _a.isDarkOverlay, onDismiss = _a.onDismiss, onLayerDidMount = _a.onLayerDidMount, onLayerMounted = _a.onLayerMounted, responsiveMode = _a.responsiveMode, subText = _a.subText, title = _a.title, type = _a.type;
        var _b = this.state, id = _b.id, isOpen = _b.isOpen, isAnimatingOpen = _b.isAnimatingOpen, isAnimatingClose = _b.isAnimatingClose;
        // @TODO - the discussion on whether the Dialog contain a property for rendering itself is still being discussed
        if (!isOpen) {
            return null;
        }
        var subTextContent;
        var dialogClassName = Utilities_1.css('ms-Dialog', Dialog_scss_1.default.root, this.props.className, (_c = {},
            _c['ms-Dialog--lgHeader ' + Dialog_scss_1.default.isLargeHeader] = type === Dialog_Props_1.DialogType.largeHeader,
            _c['ms-Dialog--close ' + Dialog_scss_1.default.isClose] = type === Dialog_Props_1.DialogType.close,
            _c['is-open ' + Dialog_scss_1.default.isOpen] = isOpen,
            _c['ms-u-fadeIn200'] = isAnimatingOpen,
            _c['ms-u-fadeOut200'] = isAnimatingClose,
            _c));
        var groupings = this._groupChildren();
        if (subText) {
            subTextContent = React.createElement("p", { className: Utilities_1.css('ms-Dialog-subText', Dialog_scss_1.default.subText), id: id + '-subText' }, subText);
        }
        // @temp tuatology - Will adjust this to be a panel at certain breakpoints
        if (responsiveMode >= withResponsiveMode_1.ResponsiveMode.small) {
            return (React.createElement(Layer_1.Layer, { onLayerDidMount: onLayerMounted || onLayerDidMount },
                React.createElement(index_2.Popup, { role: isBlocking ? 'alertdialog' : 'dialog', ariaLabelledBy: title && id + '-title', ariaDescribedBy: subText && id + '-subText', onDismiss: onDismiss },
                    React.createElement("div", { className: dialogClassName, ref: this._onDialogRef },
                        React.createElement(Overlay_1.Overlay, { isDarkThemed: isDarkOverlay, onClick: isBlocking ? null : onDismiss }),
                        React.createElement(index_1.FocusTrapZone, { className: Utilities_1.css('ms-Dialog-main', Dialog_scss_1.default.main, this.props.containerClassName), elementToFocusOnDismiss: elementToFocusOnDismiss, isClickableOutsideFocusTrap: isClickableOutsideFocusTrap ? isClickableOutsideFocusTrap : !isBlocking, ignoreExternalFocusing: ignoreExternalFocusing, forceFocusInsideTrap: forceFocusInsideTrap, firstFocusableSelector: firstFocusableSelector },
                            React.createElement("div", { className: Utilities_1.css('ms-Dialog-header', Dialog_scss_1.default.header) },
                                React.createElement("p", { className: Utilities_1.css('ms-Dialog-title', Dialog_scss_1.default.title), id: id + '-title' }, title),
                                React.createElement("div", { className: Utilities_1.css('ms-Dialog-topButton', Dialog_scss_1.default.topButton) },
                                    this.props.topButtonsProps.map(function (props) { return (React.createElement(Button_1.Button, __assign({}, props))); }),
                                    React.createElement(Button_1.Button, { className: Utilities_1.css('ms-Dialog-button ms-Dialog-button--close', Dialog_scss_1.default.button, Dialog_scss_1.default.isClose), buttonType: Button_1.ButtonType.icon, icon: 'Cancel', ariaLabel: closeButtonAriaLabel, onClick: onDismiss }))),
                            React.createElement("div", { className: Utilities_1.css('ms-Dialog-inner', Dialog_scss_1.default.inner) },
                                React.createElement("div", { className: Utilities_1.css('ms-Dialog-content', Dialog_scss_1.default.content, this.props.contentClassName) },
                                    subTextContent,
                                    groupings.contents),
                                groupings.footers))))));
        }
        var _c;
    };
    // @TODO - typing the footers as an array of DialogFooter is difficult because
    // casing "child as DialogFooter" causes a problem because
    // "Neither type 'ReactElement<any>' nor type 'DialogFooter' is assignable to the other."
    Dialog.prototype._groupChildren = function () {
        var groupings = {
            footers: [],
            contents: []
        };
        React.Children.map(this.props.children, function (child) {
            if (typeof child === 'object' && child !== null && child.type === DialogFooter_1.DialogFooter) {
                groupings.footers.push(child);
            }
            else {
                groupings.contents.push(child);
            }
        });
        return groupings;
    };
    Dialog.prototype._onDialogRef = function (ref) {
        if (ref) {
            this._events.on(ref, 'animationend', this._onAnimationEnd);
        }
        else {
            this._events.off();
        }
    };
    // Watch for completed animations and set the state
    Dialog.prototype._onAnimationEnd = function (ev) {
        // The dialog has just opened (faded in)
        if (ev.animationName.indexOf('fadeIn') > -1) {
            this.setState({
                isOpen: true,
                isAnimatingOpen: false
            });
        }
        // The dialog has just closed (faded out)
        if (ev.animationName.indexOf('fadeOut') > -1) {
            this.setState({
                isOpen: false,
                isAnimatingClose: false
            });
            // Call the onDismiss callback
            if (this.props.onDismissed) {
                this.props.onDismissed();
            }
        }
    };
    return Dialog;
}(Utilities_1.BaseComponent));
Dialog.defaultProps = {
    isOpen: false,
    type: Dialog_Props_1.DialogType.normal,
    isDarkOverlay: true,
    isBlocking: false,
    className: '',
    containerClassName: '',
    contentClassName: '',
    topButtonsProps: []
};
Dialog = __decorate([
    withResponsiveMode_1.withResponsiveMode
], Dialog);
exports.Dialog = Dialog;

//# sourceMappingURL=Dialog.js.map
