/**
 * @fileoverview added by tsickle
 * @suppress {checkTypes,extraRequire,missingOverride,missingReturn,unusedPrivateMembers,uselessCode} checked by tsc
 */
import * as tslib_1 from "tslib";
import { Component, ViewChild, Input, EventEmitter, ChangeDetectionStrategy } from '@angular/core';
import { Router } from '@angular/router';
import { LocalStorageService } from '@armor/platform-browser';
import { IdentityService, IsEmployeeService, LegacyAccountService } from '@armor/api';
import { $ } from '../../utils/jquery';
import { find } from 'lodash';
import * as natsortNamespace from 'natsort';
import { AlertService } from '../../services/alerts/alert.service';
/** @type {?} */
var natsort = natsortNamespace;
var UserMenuComponent = /** @class */ (function () {
    function UserMenuComponent(identityService, localStorageService, router, isEmployeeService, alertService, legacyAccountService) {
        var _this = this;
        this.identityService = identityService;
        this.localStorageService = localStorageService;
        this.router = router;
        this.isEmployeeService = isEmployeeService;
        this.alertService = alertService;
        this.legacyAccountService = legacyAccountService;
        this.orgManagementUrl = '/account/overview';
        this.accountChanged$ = new EventEmitter();
        this.identityService.onIdentityReady((/**
         * @param {?} me
         * @return {?}
         */
        function (me) {
            _this.me = me;
            _this.me.accounts =
                _this.me.accounts.sort((/**
                 * @param {?} a
                 * @param {?} b
                 * @return {?}
                 */
                function (a, b) { return natsort({ insensitive: true })(a.name, b.name); }));
            _this.settingsUrl = "/account/users/" + _this.me.user.id;
            _this.switchToAccount(_this.localStorageService.get(identityService.currentAccountStorageKey) || _this.me.accounts[0].id);
        }));
    }
    /**
     * @return {?}
     */
    UserMenuComponent.prototype.ngOnInit = /**
     * @return {?}
     */
    function () {
        this.isEmployee = this.isEmployeeService.isEmployee();
    };
    /**
     * @param {?} accountId
     * @return {?}
     */
    UserMenuComponent.prototype.select = /**
     * @param {?} accountId
     * @return {?}
     */
    function (accountId) {
        /** @type {?} */
        var currentAccount = this.switchToAccount(accountId);
        this.close();
        this.accountChanged$.emit(currentAccount);
    };
    /**
     * @return {?}
     */
    UserMenuComponent.prototype.close = /**
     * @return {?}
     */
    function () {
        this.menu.close();
    };
    /**
     * @param {?} isOpen
     * @return {?}
     */
    UserMenuComponent.prototype.onOpenChange = /**
     * @param {?} isOpen
     * @return {?}
     */
    function (isOpen) {
        if (!this.filterSelect) {
            return;
        }
        if (isOpen) {
            $('body').addClass('user-menu-open');
            this.filterSelect.focusFilter();
        }
        else {
            $('body').removeClass('user-menu-open');
            this.filterSelect.blurFilter();
        }
    };
    /**
     * @param {?} accountId
     * @return {?}
     */
    UserMenuComponent.prototype.switchToAccount = /**
     * @param {?} accountId
     * @return {?}
     */
    function (accountId) {
        this.currentAccount = this.getAccountById(accountId);
        this.localStorageService.set(this.identityService.currentAccountStorageKey, accountId);
        this.accountChoices = this.me.accounts.filter((/**
         * @param {?} acct
         * @return {?}
         */
        function (acct) { return acct.id !== accountId; }));
        return this.currentAccount;
    };
    /**
     * @return {?}
     */
    UserMenuComponent.prototype.manage = /**
     * @return {?}
     */
    function () {
        return tslib_1.__awaiter(this, void 0, void 0, function () {
            return tslib_1.__generator(this, function (_a) {
                switch (_a.label) {
                    case 0: return [4 /*yield*/, this.router.navigateByUrl(this.orgManagementUrl)];
                    case 1:
                        _a.sent();
                        return [2 /*return*/];
                }
            });
        });
    };
    /**
     * @return {?}
     */
    UserMenuComponent.prototype.settings = /**
     * @return {?}
     */
    function () {
        return tslib_1.__awaiter(this, void 0, void 0, function () {
            return tslib_1.__generator(this, function (_a) {
                switch (_a.label) {
                    case 0: return [4 /*yield*/, this.router.navigateByUrl(this.settingsUrl)];
                    case 1:
                        _a.sent();
                        return [2 /*return*/];
                }
            });
        });
    };
    /**
     * @param {?} accountId
     * @return {?}
     */
    UserMenuComponent.prototype.getAccountById = /**
     * @param {?} accountId
     * @return {?}
     */
    function (accountId) {
        return find(this.me.accounts, { id: accountId });
    };
    /**
     * @return {?}
     */
    UserMenuComponent.prototype.refreshAccounts = /**
     * @return {?}
     */
    function () {
        return tslib_1.__awaiter(this, void 0, void 0, function () {
            var apiCall, identity, accounts, e_1;
            var _this = this;
            return tslib_1.__generator(this, function (_a) {
                switch (_a.label) {
                    case 0:
                        apiCall = this.isEmployee ? this.identityService.getAccounts() : this.legacyAccountService.getAccounts();
                        identity = this.localStorageService.get('identity');
                        _a.label = 1;
                    case 1:
                        _a.trys.push([1, 3, , 4]);
                        return [4 /*yield*/, apiCall];
                    case 2:
                        accounts = _a.sent();
                        this.me.accounts = identity.accounts = accounts.filter((/**
                         * @param {?} m
                         * @return {?}
                         */
                        function (m) { return m.status !== 'Cancelled'; }))
                            .sort((/**
                         * @param {?} a
                         * @param {?} b
                         * @return {?}
                         */
                        function (a, b) { return natsort({ insensitive: true })(a.name, b.name); }));
                        this.accountChoices = this.me.accounts.filter((/**
                         * @param {?} acct
                         * @return {?}
                         */
                        function (acct) { return acct.id !== _this.currentAccount.id; }));
                        this.filterSelect.clearFilter();
                        this.localStorageService.set('identity', identity);
                        this.alertService.toastSuccess('Accounts Refresh', 'Accounts Refresh Successful.');
                        return [3 /*break*/, 4];
                    case 3:
                        e_1 = _a.sent();
                        this.alertService.toastError('Accounts Refresh', 'Accounts Refresh Failed.');
                        return [3 /*break*/, 4];
                    case 4: return [2 /*return*/];
                }
            });
        });
    };
    /**
     * @param {?} item
     * @param {?} filterVal
     * @return {?}
     */
    UserMenuComponent.prototype.filterByIdAndName = /**
     * @param {?} item
     * @param {?} filterVal
     * @return {?}
     */
    function (item, filterVal) {
        return item.id === Number(filterVal) || item.name.toLowerCase().includes(filterVal.toLocaleLowerCase());
    };
    UserMenuComponent.decorators = [
        { type: Component, args: [{
                    selector: 'user-menu',
                    changeDetection: ChangeDetectionStrategy.OnPush,
                    template: "<li>\n  <div #userMenu=\"ngbDropdown\" ngbDropdown class=\"d-inline-block with-caret caret-right main-user-menu\"\n    [placement]=\"'bottom-right'\"\n    [autoClose]=\"false\"\n    (openChange)=\"onOpenChange($event)\"\n    (keyup.escape)=\"close()\"\n    (clickOutside)=\"close()\">\n\n    <button class=\"toolbar-menu-item dropdown-toggle current-account\" ngbDropdownToggle>\n      <div class=\"current-account-item\">\n        <span class=\"current-account-name hidden-xxs\">{{currentAccount?.name}}</span>\n        <span class=\"current-account-number hidden-xxs\">{{currentAccount?.id}}</span>\n        <i class=\"ico ico-menu\" alt=\"Menu\" id=\"user-menu-toggle\"></i>\n      </div>\n    </button>\n\n    <div ngbDropdownMenu aria-labelledby=\"user-menu\">\n      <ng-container *ngIf=\"currentAccount\">\n        <div class=\"account-list-link current dropdown-item pb-1 pt-3 with-caret\">\n          <div class=\"no-gutters\">\n            <div class=\"col-auto\">\n              <div class=\"account-list-link-circle\">\n                {{ me.user.firstName[0] }}{{ me.user.lastName[0] }}\n              </div>\n            </div>\n            <div class=\"col\">\n              <a href=\"javascript:void(0);\" class=\"hover-action\" (click)=\"manage()\">\n                <span class=\"account-list-link-text hover-action-content pb-0\">{{ currentAccount.name }}</span>\n                <div class=\"hover-action-action\">\n                  <label>Manage Org</label> <i class=\"ico ico-edit\"></i>\n                </div>\n              </a>\n              <a href=\"javascript:void(0);\" class=\"hover-action\" (click)=\"settings()\">\n                <p class=\"hover-action-content account-list-user-name\">{{ me.user.firstName }} {{ me.user.lastName }}</p>\n                <div class=\"hover-action-action\">\n                  <label>Manage User</label> <i class=\"ico ico-account\"></i>\n                </div>\n              </a>\n            </div>\n          </div>\n        </div>\n        <div class=\"user-menu--account-list\" *ngIf=\"accountChoices?.length > 0\">\n          <filter-select\n            #filterSelect\n            [itemTemplate]=\"repeaterTemplate\"\n            [noMatchesTemplate]=\"noMatchesTemplate\"\n            [choices]=\"accountChoices\"\n            [placeholderText]=\"'Filter Accounts'\"\n            (onSelect)=\"select($event)\"\n            [filterPredicate]=\"filterByIdAndName\"\n            [refreshButtonTemplate]=\"refreshButtonTemplate\">\n          </filter-select>\n        </div>\n        <ng-content select=\"user-menu-footer\"></ng-content>\n      </ng-container>\n    </div>\n  </div>\n\n  <ng-template #repeaterTemplate let-account=\"choice\">\n    <span class=\"hover-action hover-action-primary dropdown-item account-list-link\">\n      <span class=\"account-list-link-text hover-action-content\">{{ account.name }}</span>\n      <span class=\"hover-action-action\"><label>Switch to Org</label> <i class=\"ico ico-swap\"></i></span>\n    </span>\n  </ng-template>\n\n  <ng-template #noMatchesTemplate>\n    <div class=\"dropdown-item py-4 text-center text-muted account-list-link\">\n      No Accounts Found\n    </div>\n  </ng-template>\n\n  <ng-template #refreshButtonTemplate>\n    <div class=\"input-group py-2 px-3 nav-link account-list-link-text m-0 account-list-refresh-btn\" (click)=\"refreshAccounts()\" *ngIf=\"isEmployee\">\n      <i class=\"ico ico-reboot refresh-icon\"></i><span class=\"ml-2 account-list-refresh-btn-text\"> Refresh</span>\n    </div>\n  </ng-template>\n</li>\n"
                }] }
    ];
    /** @nocollapse */
    UserMenuComponent.ctorParameters = function () { return [
        { type: IdentityService },
        { type: LocalStorageService },
        { type: Router },
        { type: IsEmployeeService },
        { type: AlertService },
        { type: LegacyAccountService }
    ]; };
    UserMenuComponent.propDecorators = {
        menu: [{ type: ViewChild, args: ['userMenu',] }],
        filterSelect: [{ type: ViewChild, args: ['filterSelect',] }],
        orgManagementUrl: [{ type: Input }],
        settingsUrl: [{ type: Input }]
    };
    return UserMenuComponent;
}());
export { UserMenuComponent };
if (false) {
    /** @type {?} */
    UserMenuComponent.prototype.menu;
    /** @type {?} */
    UserMenuComponent.prototype.filterSelect;
    /** @type {?} */
    UserMenuComponent.prototype.orgManagementUrl;
    /** @type {?} */
    UserMenuComponent.prototype.settingsUrl;
    /** @type {?} */
    UserMenuComponent.prototype.accountChanged$;
    /** @type {?} */
    UserMenuComponent.prototype.accountChoices;
    /** @type {?} */
    UserMenuComponent.prototype.currentAccount;
    /** @type {?} */
    UserMenuComponent.prototype.me;
    /** @type {?} */
    UserMenuComponent.prototype.isEmployee;
    /**
     * @type {?}
     * @private
     */
    UserMenuComponent.prototype.identityService;
    /**
     * @type {?}
     * @private
     */
    UserMenuComponent.prototype.localStorageService;
    /**
     * @type {?}
     * @private
     */
    UserMenuComponent.prototype.router;
    /**
     * @type {?}
     * @private
     */
    UserMenuComponent.prototype.isEmployeeService;
    /**
     * @type {?}
     * @private
     */
    UserMenuComponent.prototype.alertService;
    /**
     * @type {?}
     * @private
     */
    UserMenuComponent.prototype.legacyAccountService;
}
//# sourceMappingURL=data:application/json;base64, 