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 { Component, Host, h, Element, Prop, Event, State } from '@stencil/core';
import { InjectVegaGlobalSlimmer, InjectVegaSlimmer } from 'vega-slimmer/core';
import { VegaComponentUsageRuntimeMetricsSlimmer } from '../../../helpers/slimmers/component-usage-runtime-metrics';
import { sanitizeVegaComponent } from '../../../utils/component';
import { Observer } from '../../../helpers/change-manager/observer/observer';
import ChangeManager from '../../../helpers/change-manager/change-manager';
import { SIDENAV_LINK_SELECTED_TOPIC } from '../../../helpers/change-manager/subject/static-subject/static-subject-title';
import { isHTMLVegaSidenavLinkElement } from '../utils';
import { VegaSidenavLinkSubStateController } from './slimmers/controllers/vega-sidenav-link-sub-state.controller';
import { findParent } from '../../../utils/ui';
import domNodeSubjectFactory from '../../../helpers/change-manager/subject/dom-node-subject/dom-node-subject-factory';
import changeManager from '../../../helpers/change-manager/change-manager';
import { VegaInternalSidenavLinkClick } from '../../../helpers/event-manager/event-id/vega-internal-event-id';
import { createEventEmitSlimmer, } from '../../../helpers/event-manager/slimmers/event-emit-slimmer';
import { VegaClick } from '../../../helpers/event-manager/event-id/vega-event-id';
import internalVegaEventManager from '../../../helpers/event-manager/internal-vega-event-manager';
/**
 * @deprecated This component will be removed soon, please consider using `vega-left-nav-link` component instead.
 * @vegaVersion 1, v2.29.0
 */
export class VegaSidenavLink {
  constructor() {
    this.globalSlimmers = {};
    this.vegaComponentUsageRuntimeMetricsSlimmer = new VegaComponentUsageRuntimeMetricsSlimmer();
    this.subStateObserverSlimmer = new VegaSidenavLinkSubStateController();
    this.clickEventEmitter = createEventEmitSlimmer(VegaSidenavLink, VegaClick);
    this.openState = 'close';
    /**
     * Indicates whether the link item is selected.
     *
     * @vegaVersion 1, v2.29.0
     */
    this.selected = false;
    /**
     * Determines if the side nav should stay open when the
     *    sidenav-link element is clicked.
     *
     * @vegaVersion 1, v2.29.0
     */
    this.showSideBarOnClickLink = false;
    /**
     * The below method is e2e-test covered in
     * @see{module:vega-sidenav-link-click-event}
     */
    /* istanbul ignore next */
    this.handleClick = (e) => {
      if (!this.selected) {
        this.selected = true;
      }
      this.clickEventEmitter.emit();
      this.notifyParentSidenavClick();
      if (internalVegaEventManager.getSemantics(this.host).namespace === 'native') {
        e.stopPropagation();
      }
    };
  }
  /**
   * The below method is e2e-test covered in
   * @see{module:vega-sidenav-link-e2e-disconnected}
   */
  /* istanbul ignore next */
  disconnectedCallback() {
    ChangeManager.unregister(SIDENAV_LINK_SELECTED_TOPIC, this.observer);
  }
  componentWillLoad() {
    const parent = findParent(this.host, 'vega-sidenav-group');
    if (parent) {
      this.host.classList.add('has-group-parent');
    }
    this.observer = new Observer(isHTMLVegaSidenavLinkElement, this.observerListener.bind(this));
    ChangeManager.register(SIDENAV_LINK_SELECTED_TOPIC, this.observer);
  }
  componentDidRender() {
    if (this.selected) {
      ChangeManager.notify(SIDENAV_LINK_SELECTED_TOPIC, this.host);
    }
  }
  render() {
    const url = this.url || 'javascript:void(0);';
    return sanitizeVegaComponent(h(Host, { class: { 'sidenav-open': this.openState !== 'close' } },
      h("a", { href: url, onClick: this.handleClick, class: { selected: this.selected === true } },
        this.icon ? (h("div", { class: "vega-links-icon" },
          h("div", { class: "prefix-icon" },
            h("vega-icon", { icon: this.icon, size: "size-24" })))) : null,
        h("span", { class: "vega-links-name" },
          h("slot", null)))), this.host);
  }
  /**
   * The below method is e2e-test covered in
   * @see{module:vega-sidenav-link-observer-with-click}
   */
  /* istanbul ignore next */
  observerListener(castInput) {
    if (castInput !== this.host) {
      this.selected = false;
    }
  }
  /**
   * Notify the parent sidenav element the child link has been clicked
   */
  notifyParentSidenavClick() {
    const sidenav = findParent(this.host, 'vega-sidenav');
    if (sidenav) {
      changeManager.notify(domNodeSubjectFactory.getSubject(sidenav, VegaInternalSidenavLinkClick), {
        host: this.host,
        detail: this.showSideBarOnClickLink,
      });
    }
  }
  static get is() { return "vega-sidenav-link"; }
  static get encapsulation() { return "shadow"; }
  static get originalStyleUrls() { return {
    "$": ["vega-sidenav-link.scss"]
  }; }
  static get styleUrls() { return {
    "$": ["vega-sidenav-link.css"]
  }; }
  static get properties() { return {
    "icon": {
      "type": "string",
      "mutable": false,
      "complexType": {
        "original": "string",
        "resolved": "string",
        "references": {}
      },
      "required": false,
      "optional": false,
      "docs": {
        "tags": [{
            "name": "vegaVersion",
            "text": "1, v2.29.0"
          }],
        "text": "Specifies the icon displayed for the side navigation link.\n   Icons are only displayed for top-level links."
      },
      "attribute": "icon",
      "reflect": false
    },
    "selected": {
      "type": "boolean",
      "mutable": true,
      "complexType": {
        "original": "boolean",
        "resolved": "boolean",
        "references": {}
      },
      "required": false,
      "optional": false,
      "docs": {
        "tags": [{
            "name": "vegaVersion",
            "text": "1, v2.29.0"
          }],
        "text": "Indicates whether the link item is selected."
      },
      "attribute": "selected",
      "reflect": true,
      "defaultValue": "false"
    },
    "url": {
      "type": "string",
      "mutable": false,
      "complexType": {
        "original": "string",
        "resolved": "string",
        "references": {}
      },
      "required": false,
      "optional": false,
      "docs": {
        "tags": [{
            "name": "vegaVersion",
            "text": "1, v2.29.0"
          }],
        "text": "Specifies the destination URL to which the side navigation link\n   navigates when clicked."
      },
      "attribute": "url",
      "reflect": false
    },
    "showSideBarOnClickLink": {
      "type": "boolean",
      "mutable": false,
      "complexType": {
        "original": "boolean",
        "resolved": "boolean",
        "references": {}
      },
      "required": false,
      "optional": false,
      "docs": {
        "tags": [{
            "name": "vegaVersion",
            "text": "1, v2.29.0"
          }],
        "text": "Determines if the side nav should stay open when the\n   sidenav-link element is clicked."
      },
      "attribute": "show-side-bar-on-click-link",
      "reflect": false,
      "defaultValue": "false"
    }
  }; }
  static get states() { return {
    "openState": {}
  }; }
  static get events() { return [{
      "method": "vegaClick",
      "name": "vegaClick",
      "bubbles": true,
      "cancelable": true,
      "composed": true,
      "docs": {
        "tags": [{
            "name": "vegaVersion",
            "text": "1, v2.29.0"
          }],
        "text": "An event emitter notifying about the occurrence of a click event."
      },
      "complexType": {
        "original": "any",
        "resolved": "any",
        "references": {}
      }
    }, {
      "method": "click",
      "name": "click",
      "bubbles": true,
      "cancelable": true,
      "composed": true,
      "docs": {
        "tags": [{
            "name": "eventSemantics",
            "text": "namespace:native"
          }, {
            "name": "vegaVersion",
            "text": "1, v2.29.0"
          }],
        "text": "An event emitter notifying about the occurrence of a click event."
      },
      "complexType": {
        "original": "any",
        "resolved": "any",
        "references": {}
      }
    }]; }
  static get elementRef() { return "host"; }
}
__decorate([
  InjectVegaGlobalSlimmer()
], VegaSidenavLink.prototype, "globalSlimmers", void 0);
__decorate([
  InjectVegaSlimmer()
], VegaSidenavLink.prototype, "vegaComponentUsageRuntimeMetricsSlimmer", void 0);
__decorate([
  InjectVegaSlimmer()
], VegaSidenavLink.prototype, "subStateObserverSlimmer", void 0);
__decorate([
  InjectVegaSlimmer()
], VegaSidenavLink.prototype, "clickEventEmitter", void 0);
