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;
};
import { MapToComponentField, MapToComponentMethod } from 'vega-slimmer/core';
import ChangeManager from '../../../../../helpers/change-manager/change-manager';
import { VegaNavObserverSlimmer } from '../../../slimmers/vega-nav-observer-slimmer';
import domNodeSubjectFactory from '../../../../../helpers/change-manager/subject/dom-node-subject/dom-node-subject-factory';
import { VegaInternalInitializeLeftNavLinkSelected, VegaInternalLeftNavLinkSelectedTopic, } from '../../../../../helpers/event-manager/event-id/vega-internal-event-id';
export class VegaLeftNavLinkController extends VegaNavObserverSlimmer {
  /**
   * When selected is true during initialization, the VegaInternalInitializeLeftNavLinkSelected observer needs to be notified
   */
  componentWillRender() {
    if (this.selected) {
      const topParent = this.vegaLeftNavHierarchyController.getTopParent();
      if (topParent) {
        ChangeManager.notify(domNodeSubjectFactory.getSubject(topParent, VegaInternalInitializeLeftNavLinkSelected), {
          host: this.host,
        });
      }
    }
  }
  componentDidRender() {
    if (this.selected) {
      const subject = this.getObserverSubject();
      if (subject) {
        ChangeManager.notify(subject, this.host);
      }
    }
  }
  /**
   * The below method is e2e-test covered in
   * @see{module:vega-left-nav-link-observer-with-click}
   */
  /* istanbul ignore next */
  setSelected(selected) {
    if (!this.customSelectionEnabled) {
      this.selected = selected;
    }
  }
  /**
   * The below method is e2e-test covered in
   * @see{module:vega-left-nav-link-observer-with-click}
   */
  /* istanbul ignore next */
  observerListener(castInput) {
    if (castInput !== this.host) {
      this.setSelected(false);
    }
  }
  /**
   * Returns the dom node subject for selection observer registration.
   * The subject is scoped to the top parent nav container for this link.
   */
  getObserverSubject() {
    const topParent = this.vegaLeftNavHierarchyController.getTopParent();
    return topParent
      ? domNodeSubjectFactory.getSubject(topParent, VegaInternalLeftNavLinkSelectedTopic)
      : null;
  }
}
__decorate([
  MapToComponentField()
], VegaLeftNavLinkController.prototype, "host", void 0);
__decorate([
  MapToComponentField({ writable: true })
], VegaLeftNavLinkController.prototype, "selected", void 0);
__decorate([
  MapToComponentField()
], VegaLeftNavLinkController.prototype, "customSelectionEnabled", void 0);
__decorate([
  MapToComponentField()
], VegaLeftNavLinkController.prototype, "vegaLeftNavHierarchyController", void 0);
__decorate([
  MapToComponentMethod('componentWillRender')
], VegaLeftNavLinkController.prototype, "componentWillRender", null);
__decorate([
  MapToComponentMethod('componentDidRender')
], VegaLeftNavLinkController.prototype, "componentDidRender", null);
