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 __());
};
define(["require", "exports", 'react', '../../FocusZone', './Nav.scss'], function (require, exports, React, FocusZone_1) {
    "use strict";
    var Nav = (function (_super) {
        __extends(Nav, _super);
        function Nav() {
            _super.call(this);
            this.state = {
                isGroupExpanded: []
            };
        }
        Nav.prototype.render = function () {
            var _this = this;
            if (!this.props.groups) {
                return null;
            }
            var groupElements = this.props.groups.map(function (group, groupIndex) { return _this._renderGroup(group, groupIndex); });
            return (React.createElement(FocusZone_1.FocusZone, {direction: FocusZone_1.FocusZoneDirection.vertical}, React.createElement("nav", {role: 'navigation', className: 'ms-Nav' + (this.props.isOnTop ? ' is-onTop ms-u-slideRightIn40' : '')}, groupElements)));
        };
        Nav.prototype._renderLink = function (link, linkIndex) {
            return (React.createElement("li", {key: linkIndex}, React.createElement("a", {className: 'ms-Nav-link' + (_isLinkSelected(link) ? ' is-selected' : ''), href: link.url || 'javascript:', onClick: this.props.onLinkClick, title: link.name}, (link.iconClassName ?
                React.createElement("i", {className: 'ms-Icon ms-Nav-IconLink ' + link.iconClassName})
                : ''), this.props.onRenderLink(link)), " ", this._renderLinks(link.links)));
        };
        Nav.prototype._renderLinks = function (links) {
            var _this = this;
            if (!links || !links.length) {
                return null;
            }
            var linkElements = links.map(function (link, linkIndex) { return _this._renderLink(link, linkIndex); });
            return (React.createElement("ul", null, linkElements));
        };
        Nav.prototype._renderGroup = function (group, groupIndex) {
            var isGroupExpanded = this.state.isGroupExpanded[groupIndex] !== false;
            return (React.createElement("div", {key: groupIndex, className: 'ms-Nav-group' + (isGroupExpanded ? ' is-expanded' : '')}, (group.name ?
                React.createElement("button", {className: 'ms-Nav-groupButton', onClick: this._onGroupHeaderClicked.bind(this, groupIndex)}, React.createElement("i", {className: 'ms-Nav-groupChevron ms-Icon ms-Icon--chevronDown'}), group.name) : null), React.createElement("div", {className: 'ms-Nav-groupContent ms-u-slideDownIn20'}, this._renderLinks(group.links))));
        };
        Nav.prototype._onGroupHeaderClicked = function (groupIndex, ev) {
            var currentState = this.state.isGroupExpanded[groupIndex] !== false;
            this.state.isGroupExpanded[groupIndex] = !currentState;
            this.forceUpdate();
            ev.preventDefault();
            ev.stopPropagation();
        };
        Nav.defaultProps = {
            groups: null,
            onRenderLink: function (link) { return (React.createElement("span", {className: 'ms-Nav-linkText'}, link.name)); }
        };
        return Nav;
    }(React.Component));
    exports.Nav = Nav;
    // A tag used for resolving links.
    var _urlResolver = document.createElement('a');
    function _isLinkSelected(link) {
        if (!link.url) {
            return false;
        }
        _urlResolver.href = link.url || '';
        var target = _urlResolver.href;
        if (location.protocol + '//' + location.host + location.pathname === target) {
            return true;
        }
        if (location.href === target) {
            return true;
        }
        if (location.hash) {
            // Match the hash to the url.
            if (location.hash === link.url) {
                return true;
            }
            // Match a rebased url. (e.g. #foo becomes http://hostname/foo)
            _urlResolver.href = location.hash.substring(1);
            return _urlResolver.href === target;
        }
        return false;
    }
});
