text: /**
   * @license
   * Copyright 2018 Google Inc.
   *
   * Permission is hereby granted, free of charge, to any person obtaining a copy
   * of this software and associated documentation files (the "Software"), to deal
   * in the Software without restriction, including without limitation the rights
   * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
   * copies of the Software, and to permit persons to whom the Software is
   * furnished to do so, subject to the following conditions:
   *
   * The above copyright notice and this permission notice shall be included in
   * all copies or substantial portions of the Software.
   *
   * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
   * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
   * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
   * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
   * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
   * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
   * THE SOFTWARE.
   */
  import * as tslib_1 from "tslib";
  import { MDCFoundation } from '@material/base/foundation';
  import { MDCListFoundation } from '@material/list/foundation';
  import { MDCMenuSurfaceFoundation } from '@material/menu-surface/foundation';
  import { cssClasses, strings } from './constants';
  var MDCMenuFoundation = /** @class */ (function (_super) {
      tslib_1.__extends(MDCMenuFoundation, _super);
      function MDCMenuFoundation(adapter) {
          var _this = _super.call(this, tslib_1.__assign({}, MDCMenuFoundation.defaultAdapter, adapter)) || this;
          _this.closeAnimationEndTimerId_ = 0;
          return _this;
      }
      Object.defineProperty(MDCMenuFoundation, "cssClasses", {
          get: function () {
              return cssClasses;
          },
          enumerable: true,
          configurable: true
      });
      Object.defineProperty(MDCMenuFoundation, "strings", {
          get: function () {
              return strings;
          },
          enumerable: true,
          configurable: true
      });
      Object.defineProperty(MDCMenuFoundation, "defaultAdapter", {
          /**
           * @see {@link MDCMenuAdapter} for typing information on parameters and return types.
           */
          get: function () {
              // tslint:disable:object-literal-sort-keys Methods should be in the same order as the adapter interface.
              return {
                  addClassToElementAtIndex: function () { return undefined; },
                  removeClassFromElementAtIndex: function () { return undefined; },
                  addAttributeToElementAtIndex: function () { return undefined; },
                  removeAttributeFromElementAtIndex: function () { return undefined; },
                  elementContainsClass: function () { return false; },
                  closeSurface: function () { return undefined; },
                  getElementIndex: function () { return -1; },
                  getParentElement: function () { return null; },
                  getSelectedElementIndex: function () { return -1; },
                  notifySelected: function () { return undefined; },
              };
              // tslint:enable:object-literal-sort-keys
          },
          enumerable: true,
          configurable: true
      });
      MDCMenuFoundation.prototype.destroy = function () {
          if (this.closeAnimationEndTimerId_) {
              clearTimeout(this.closeAnimationEndTimerId_);
          }
          this.adapter_.closeSurface();
      };
      MDCMenuFoundation.prototype.handleKeydown = function (evt) {
          var key = evt.key, keyCode = evt.keyCode;
          var isTab = key === 'Tab' || keyCode === 9;
          if (isTab) {
              this.adapter_.closeSurface();
          }
      };
      MDCMenuFoundation.prototype.handleItemAction = function (listItem) {
          var _this = this;
          var index = this.adapter_.getElementIndex(listItem);
          if (index < 0) {
              return;
          }
          this.adapter_.notifySelected({ index: index });
          this.adapter_.closeSurface();
          // Wait for the menu to close before adding/removing classes that affect styles.
          this.closeAnimationEndTimerId_ = setTimeout(function () {
              var selectionGroup = _this.getSelectionGroup_(listItem);
              if (selectionGroup) {
                  _this.handleSelectionGroup_(selectionGroup, index);
              }
          }, MDCMenuSurfaceFoundation.numbers.TRANSITION_CLOSE_DURATION);
      };
      /**
       * Handles toggling the selected classes in a selection group when a selection is made.
       */
      MDCMenuFoundation.prototype.handleSelectionGroup_ = function (selectionGroup, index) {
          // De-select the previous selection in this group.
          var selectedIndex = this.adapter_.getSelectedElementIndex(selectionGroup);
          if (selectedIndex >= 0) {
              this.adapter_.removeAttributeFromElementAtIndex(selectedIndex, strings.ARIA_SELECTED_ATTR);
              this.adapter_.removeClassFromElementAtIndex(selectedIndex, cssClasses.MENU_SELECTED_LIST_ITEM);
          }
          // Select the new list item in this group.
          this.adapter_.addClassToElementAtIndex(index, cssClasses.MENU_SELECTED_LIST_ITEM);
          this.adapter_.addAttributeToElementAtIndex(index, strings.ARIA_SELECTED_ATTR, 'true');
      };
      /**
       * Returns the parent selection group of an element if one exists.
       */
      MDCMenuFoundation.prototype.getSelectionGroup_ = function (listItem) {
          var parent = this.adapter_.getParentElement(listItem);
          if (!parent) {
              return null;
          }
          var isGroup = this.adapter_.elementContainsClass(parent, cssClasses.MENU_SELECTION_GROUP);
          // Iterate through ancestors until we find the group or get to the list.
          while (!isGroup && parent && !this.adapter_.elementContainsClass(parent, MDCListFoundation.cssClasses.ROOT)) {
              parent = this.adapter_.getParentElement(parent);
              isGroup = parent ? this.adapter_.elementContainsClass(parent, cssClasses.MENU_SELECTION_GROUP) : false;
          }
          if (isGroup) {
              return parent;
          }
          else {
              return null;
          }
      };
      return MDCMenuFoundation;
  }(MDCFoundation));
  export { MDCMenuFoundation };
  // tslint:disable-next-line:no-default-export Needed for backward compatibility with MDC Web v0.44.0 and earlier.
  export default MDCMenuFoundation;
  //# sourceMappingURL=foundation.js.map