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, VegaSlimmer } from 'vega-slimmer/core';
import internalVegaZIndexManager from '../../../../helpers/zindex/internal-vega-z-index-manager';
import { getCurrentBreakpoint } from '../../../../utils/breakpoints';
/** Manages the visibility and z-index of a backdrop element for a side navigation menu. */
export class VegaSidenavBackdropController extends VegaSlimmer {
  constructor() {
    super(...arguments);
    this.backdropRef = null;
  }
  /*
   * The below method is e2e-test covered in
   * @see{module:vega-sidenav-e2e-vega-click-backdrop-mobile}
   * @see{module:vega-sidenav-e2e-vega-resize-with-desktop-to-mobile}
   */
  /* istanbul ignore next */
  /**
   * create or remove the sidenav backdrop element.
   *
   * @param {ResponsiveBehavior} responsiveBehavior The sidenav config
   */
  updateBackdropAvailability(responsiveBehavior) {
    if (this.openState === 'open' && (responsiveBehavior === null || responsiveBehavior === void 0 ? void 0 : responsiveBehavior.showBackdrop)) {
      this.append();
    }
    else {
      this.removeZIndex('base');
      this.remove();
    }
  }
  /**
   * The function sets the z-index of an element using a specified level.
   *
   * @param {ZIndexLevelType} [level] - The zindex level
   */
  setZIndex(level) {
    internalVegaZIndexManager.setZIndex(this.backdropRef, level);
  }
  /**
   * The function removes a specified Z index level from a backdrop element using an internal Vega Z index manager.
   *
   * @param {ZIndexLevelType} [level] - The zindex level
   */
  removeZIndex(level) {
    internalVegaZIndexManager.removeZIndex(this.backdropRef, level);
  }
  /*
   * The below method is e2e-test covered in
   * @see{module:vega-sidenav-e2e-vega-click-backdrop-mobile}
   */
  /* istanbul ignore next */
  /**
   * Appends a backdrop element to the body of the document and sets its visibility based on a breakpoint.
   */
  append() {
    // eslint-disable-next-line no-restricted-globals
    let backdrop = document.querySelector(`vega-backdrop.${VegaSidenavBackdropController.backdropClassName}`);
    if (!backdrop) {
      // eslint-disable-next-line no-restricted-globals
      backdrop = document.createElement('vega-backdrop');
      backdrop.classList.add(VegaSidenavBackdropController.backdropClassName);
      // eslint-disable-next-line no-restricted-globals
      document.querySelector('body').append(backdrop);
    }
    backdrop.visible = this.getBackdropVisibleByBreakpoint();
    this.backdropRef = backdrop;
  }
  /*
   * The below method is e2e-test covered in
   * @see{module:vega-sidenav-e2e-vega-click-backdrop-mobile}
   */
  /* istanbul ignore next */
  /**
   * Sets the visibility of the `backdropRef` to false.
   */
  remove() {
    if (this.backdropRef) {
      this.backdropRef.remove();
    }
  }
  /*
   * The below method is e2e-test covered in
   * @see{module:vega-sidenav-e2e-vega-click-backdrop-mobile}
   */
  /* istanbul ignore next */
  /**
   * Determines if the backdrop is visible based on the current breakpoint.
   *
   * @returns {boolean} A boolean value based on the current breakpoint. It returns `true` if the current breakpoint is either 'S' or 'M', and `false` otherwise.
   */
  getBackdropVisibleByBreakpoint() {
    const breakpoint = getCurrentBreakpoint();
    return breakpoint === 'S' || breakpoint === 'M' ? true : false;
  }
}
VegaSidenavBackdropController.backdropClassName = 'vega-sidenav-backdrop';
__decorate([
  MapToComponentField()
], VegaSidenavBackdropController.prototype, "openState", void 0);
