"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 __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;
};
/* tslint:disable:no-unused-variable */
var React = require('react');
var DirectionalHint_1 = require('../../common/DirectionalHint');
var Utilities_1 = require('../../Utilities');
var positioning_1 = require('../../utilities/positioning');
var focus_1 = require('../../utilities/focus');
var index_1 = require('../Popup/index');
var BaseComponent_1 = require('../../common/BaseComponent');
require('./Callout.scss');
var BEAK_ORIGIN_POSITION = { top: 0, left: 0 };
var OFF_SCREEN_POSITION = { top: -9999, left: 0 };
var CalloutContent = (function (_super) {
    __extends(CalloutContent, _super);
    function CalloutContent(props) {
        _super.call(this, props);
        this._didSetInitialFocus = false;
        this.state = {
            positions: null,
            slideDirectionalClassName: null,
            calloutElementRect: null
        };
    }
    CalloutContent.prototype.componentDidUpdate = function () {
        this._setInitialFocus();
        this._updatePosition();
    };
    CalloutContent.prototype.componentDidMount = function () {
        this._onComponentDidMount();
    };
    CalloutContent.prototype.render = function () {
        var _this = this;
        var _a = this.props, className = _a.className, targetElement = _a.targetElement, isBeakVisible = _a.isBeakVisible, beakStyle = _a.beakStyle, children = _a.children;
        var _b = this.state, positions = _b.positions, slideDirectionalClassName = _b.slideDirectionalClassName;
        var content = (React.createElement("div", {ref: this._resolveRef('_hostElement'), className: 'ms-Callout-container'}, React.createElement("div", {className: Utilities_1.css('ms-Callout', className, slideDirectionalClassName ? "ms-u-" + slideDirectionalClassName : ''), style: ((positions) ? positions.callout : OFF_SCREEN_POSITION), ref: this._resolveRef('_calloutElement')}, isBeakVisible && targetElement ? (React.createElement("div", {className: beakStyle, style: ((positions) ? positions.beak : BEAK_ORIGIN_POSITION)})) : (null), React.createElement("div", {className: 'ms-Callout-beakCurtain'}), React.createElement(index_1.Popup, {className: 'ms-Callout-main', onDismiss: function (ev) { return _this.dismiss(); }, shouldRestoreFocus: true}, children))));
        return content;
    };
    CalloutContent.prototype.dismiss = function () {
        var onDismiss = this.props.onDismiss;
        if (onDismiss) {
            onDismiss();
        }
    };
    CalloutContent.prototype._dismissOnLostFocus = function (ev) {
        var targetElement = this.props.targetElement;
        var target = ev.target;
        if (ev.target !== window &&
            this._hostElement &&
            !Utilities_1.elementContains(this._hostElement, target) &&
            (!targetElement || !Utilities_1.elementContains(targetElement, target))) {
            this.dismiss();
        }
    };
    CalloutContent.prototype._setInitialFocus = function () {
        if (this.props.setInitialFocus && !this._didSetInitialFocus && this.state.positions) {
            this._didSetInitialFocus = true;
            focus_1.focusFirstChild(this._calloutElement);
        }
    };
    CalloutContent.prototype._onComponentDidMount = function () {
        // This is added so the callout will dismiss when the window is scrolled
        // but not when something inside the callout is scrolled.
        this._events.on(window, 'scroll', this._dismissOnLostFocus, true);
        this._events.on(window, 'resize', this.dismiss, true);
        this._events.on(window, 'focus', this._dismissOnLostFocus, true);
        this._events.on(window, 'click', this._dismissOnLostFocus, true);
        if (this.props.onLayerMounted) {
            this.props.onLayerMounted();
        }
        this._updatePosition();
    };
    CalloutContent.prototype._updatePosition = function () {
        var positions = this.state.positions;
        var hostElement = this._hostElement;
        var calloutElement = this._calloutElement;
        if (hostElement && calloutElement) {
            var positionInfo = positioning_1.getRelativePositions(this.props, hostElement, calloutElement);
            // Set the new position only when the positions are not exists or one of the new callout positions are different
            if ((!positions && positionInfo) ||
                (positions && positionInfo && (positions.callout.top !== positionInfo.calloutPosition.top || positions.callout.left !== positionInfo.calloutPosition.left))) {
                this.setState({
                    positions: {
                        callout: positionInfo.calloutPosition,
                        beak: positionInfo.beakPosition,
                    },
                    slideDirectionalClassName: positionInfo.directionalClassName
                });
            }
        }
    };
    CalloutContent.defaultProps = {
        isBeakVisible: true,
        beakStyle: 'ms-Callout-beak',
        beakWidth: 28,
        gapSpace: 0,
        directionalHint: Utilities_1.getRTL() ? DirectionalHint_1.DirectionalHint.bottomRightEdge : DirectionalHint_1.DirectionalHint.bottomLeftEdge
    };
    __decorate([
        Utilities_1.autobind
    ], CalloutContent.prototype, "_setInitialFocus", null);
    __decorate([
        Utilities_1.autobind
    ], CalloutContent.prototype, "_onComponentDidMount", null);
    return CalloutContent;
}(BaseComponent_1.BaseComponent));
exports.CalloutContent = CalloutContent;

//# sourceMappingURL=CalloutContent.js.map
