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, Prop, Watch, Element, Event, Method, State } from '@stencil/core';
import { sanitizeVegaComponent } from '../../utils/component';
import { InjectKeyboardManager } from '../../decorators/property-decorators/inject-keyboard-manager';
import { InjectVegaGlobalSlimmer, InjectVegaSlimmer, methodPlaceholder, } from 'vega-slimmer/core';
import { createEventEmitSlimmer, } from '../../helpers/event-manager/slimmers/event-emit-slimmer';
import { VegaPopoverShow, VegaPopoverHide } from '../../helpers/event-manager/event-id/vega-event-id';
import { VegaPopoverAppenderRenderer } from './slimmers/renderers/vega-popover-appender-renderer';
import { VegaPopoverAppenderController } from './slimmers/controllers/vega-popover-appender-controller';
import { VegaComponentUsageRuntimeMetricsSlimmer } from '../../helpers/slimmers/component-usage-runtime-metrics';
/**
 * @vegaVersion 1.19.0
 */
export class VegaPopover {
  constructor() {
    this.globalSlimmers = {};
    this.appenderRenderer = new VegaPopoverAppenderRenderer();
    this.appenderController = new VegaPopoverAppenderController();
    this.popoverShowEventEmitter = createEventEmitSlimmer(VegaPopover, VegaPopoverShow);
    this.popoverHideEventEmitter = createEventEmitSlimmer(VegaPopover, VegaPopoverHide);
    this.vegaComponentUsageRuntimeMetricsSlimmer = new VegaComponentUsageRuntimeMetricsSlimmer();
    /** popover isPopoverShow */
    this.isPopoverShow = false;
    /**
     * Specifies the placement or position of the popover
     *    in relation to its parent element.
     *
     * If there is insufficient space to display the popover in the
     *    default position, it will automatically adjust its placement
     *    based on the following order: `top`, `left`, `bottom`, `right`.
     *
     * @vegaVersion 1.19.0
     */
    this.placement = 'top';
    /**
     * Controls how the popover aligns horizontally and vertically,
     *    ensuring it is displayed in the desired position.
     *
     * @vegaVersion 1.19.0
     */
    this.alignment = 'center';
    /**
     * Specifies the trigger mode for the popover.
     *
     * @vegaVersion 2.1.0
     */
    this.trigger = 'click';
    /**
     * Controls whether the arrow is display.
     *
     * @vegaVersion 2.8.0
     */
    this.showArrow = true;
    /**
     * Determines whether the popover's position is calculated relative to the screen.
     *
     * @VegaWarning - It will overrides the positionRelativeTo and matchContainerHeight properties, rendering them ineffective.
     *              - Currently, when set to true, the position calculation may be inaccurate if the popover is placed inside a scrollable container within a shadowRoot (e.g., inside a vega-modal with scrollable content).
     * @vegaVersion 2.39.0
     */
    this.isScreenPosition = false;
    /**
     * Determines the spacing between the content and the edges of the popover content box.
     *
     * @vegaVersion 2.59.0
     */
    this.padding = 'size-16';
    /* istanbul ignore next */
    this.showPopover = () => {
      this.appenderController.show();
    };
    /* istanbul ignore next */
    this.hidePopover = () => {
      this.appenderController.hide();
    };
  }
  async watchIsPopoverShow() {
    if (this.isPopoverShow) {
      this.popoverShowEventEmitter.emit();
    }
    else {
      this.popoverHideEventEmitter.emit();
    }
  }
  watchToRefreshPopover() {
    this.isPopoverShow && this.appenderController.show();
  }
  componentDidRender() {
    methodPlaceholder();
  }
  /**
   * Invoke this method to show the popover.
   *
   * @vegaVersion 1.22.0
   * @param {boolean} refresh - Determines whether the popover should be refreshed even when it is already visible. This is useful for updating the popover‘s styles (such as width, height, or position). The default value is false. Added in 2.73.0.
   */
  async show(refresh = false) {
    if (!this.isPopoverShow || refresh) {
      this.showPopover();
    }
  }
  /**
   * Invoke this method to close the popover.
   *
   * @vegaVersion 1.22.0
   */
  async hide() {
    if (this.isPopoverShow) {
      this.hidePopover();
    }
  }
  render() {
    return sanitizeVegaComponent(h(Host, null,
      h("slot", { name: "popover-content" }),
      this.appenderRenderer.render()), this.host);
  }
  static get is() { return "vega-popover"; }
  static get encapsulation() { return "shadow"; }
  static get originalStyleUrls() { return {
    "$": ["vega-popover.scss"]
  }; }
  static get styleUrls() { return {
    "$": ["vega-popover.css"]
  }; }
  static get properties() { return {
    "size": {
      "type": "any",
      "mutable": false,
      "complexType": {
        "original": "ResponsiveType<SizeType>",
        "resolved": "`${number}%` | number | { S?: SizeType; M?: SizeType; L?: SizeType; XL?: SizeType; default?: SizeType; }",
        "references": {
          "ResponsiveType": {
            "location": "import",
            "path": "../../types/ui.type"
          },
          "SizeType": {
            "location": "import",
            "path": "../../types/ui.type"
          }
        }
      },
      "required": false,
      "optional": false,
      "docs": {
        "tags": [{
            "name": "vegaVersion",
            "text": "1.19.0"
          }],
        "text": "Specifies the size of the popover component.\n\nIt is provided as a number or ResponsiveType."
      },
      "attribute": "size",
      "reflect": false
    },
    "placement": {
      "type": "string",
      "mutable": false,
      "complexType": {
        "original": "PlacementType",
        "resolved": "\"bottom\" | \"left\" | \"right\" | \"top\"",
        "references": {
          "PlacementType": {
            "location": "import",
            "path": "../../types/ui.type"
          }
        }
      },
      "required": false,
      "optional": false,
      "docs": {
        "tags": [{
            "name": "vegaVersion",
            "text": "1.19.0"
          }],
        "text": "Specifies the placement or position of the popover\n   in relation to its parent element.\n\nIf there is insufficient space to display the popover in the\n   default position, it will automatically adjust its placement\n   based on the following order: `top`, `left`, `bottom`, `right`."
      },
      "attribute": "placement",
      "reflect": false,
      "defaultValue": "'top'"
    },
    "alignment": {
      "type": "string",
      "mutable": false,
      "complexType": {
        "original": "PositionAlignmentType",
        "resolved": "\"center\" | \"end\" | \"start\"",
        "references": {
          "PositionAlignmentType": {
            "location": "import",
            "path": "../../types/ui.type"
          }
        }
      },
      "required": false,
      "optional": false,
      "docs": {
        "tags": [{
            "name": "vegaVersion",
            "text": "1.19.0"
          }],
        "text": "Controls how the popover aligns horizontally and vertically,\n   ensuring it is displayed in the desired position."
      },
      "attribute": "alignment",
      "reflect": false,
      "defaultValue": "'center'"
    },
    "positionRelativeTo": {
      "type": "string",
      "mutable": false,
      "complexType": {
        "original": "string",
        "resolved": "string",
        "references": {}
      },
      "required": false,
      "optional": false,
      "docs": {
        "tags": [{
            "name": "VegaWarning",
            "text": "The value of this property should be an element that is\nthe parent of this component or an ancestor of the parent element.\nUsing any other element may lead to unexpected behavior."
          }, {
            "name": "vegaVersion",
            "text": "1.19.0"
          }],
        "text": "Specifies the selector for the element that will\n   contain the popover.\n\nIf this parameter is not provided, the popover will be appended to\n   the body element of the page. However, if you encounter any issues\n   with the positioning of the popover, especially when scrolling occurs,\n   you can modify this property to specify a different element\n   as the container for the popover."
      },
      "attribute": "position-relative-to",
      "reflect": false
    },
    "trigger": {
      "type": "string",
      "mutable": false,
      "complexType": {
        "original": "'hover' | 'click'",
        "resolved": "\"click\" | \"hover\"",
        "references": {}
      },
      "required": false,
      "optional": false,
      "docs": {
        "tags": [{
            "name": "vegaVersion",
            "text": "2.1.0"
          }],
        "text": "Specifies the trigger mode for the popover."
      },
      "attribute": "trigger",
      "reflect": false,
      "defaultValue": "'click'"
    },
    "showArrow": {
      "type": "boolean",
      "mutable": false,
      "complexType": {
        "original": "boolean",
        "resolved": "boolean",
        "references": {}
      },
      "required": false,
      "optional": false,
      "docs": {
        "tags": [{
            "name": "vegaVersion",
            "text": "2.8.0"
          }],
        "text": "Controls whether the arrow is display."
      },
      "attribute": "show-arrow",
      "reflect": false,
      "defaultValue": "true"
    },
    "isScreenPosition": {
      "type": "boolean",
      "mutable": false,
      "complexType": {
        "original": "boolean",
        "resolved": "boolean",
        "references": {}
      },
      "required": false,
      "optional": false,
      "docs": {
        "tags": [{
            "name": "VegaWarning",
            "text": "- It will overrides the positionRelativeTo and matchContainerHeight properties, rendering them ineffective.\n- Currently, when set to true, the position calculation may be inaccurate if the popover is placed inside a scrollable container within a shadowRoot (e.g., inside a vega-modal with scrollable content)."
          }, {
            "name": "vegaVersion",
            "text": "2.39.0"
          }],
        "text": "Determines whether the popover's position is calculated relative to the screen."
      },
      "attribute": "is-screen-position",
      "reflect": false,
      "defaultValue": "false"
    },
    "padding": {
      "type": "string",
      "mutable": false,
      "complexType": {
        "original": "ResponsiveType<PaddingType>",
        "resolved": "\"0\" | \"size-112\" | \"size-12\" | \"size-16\" | \"size-2\" | \"size-20\" | \"size-24\" | \"size-32\" | \"size-4\" | \"size-40\" | \"size-48\" | \"size-64\" | \"size-72\" | \"size-8\" | \"size-80\" | { S?: PaddingType; M?: PaddingType; L?: PaddingType; XL?: PaddingType; default?: PaddingType; } | { left?: \"0\" | \"size-4\" | \"size-8\" | \"size-12\" | \"size-16\" | \"size-20\" | \"size-24\" | \"size-32\" | \"size-40\" | \"size-48\" | \"size-64\" | \"size-72\" | \"size-80\" | \"size-112\" | \"size-2\"; right?: \"0\" | \"size-4\" | \"size-8\" | \"size-12\" | \"size-16\" | \"size-20\" | \"size-24\" | \"size-32\" | \"size-40\" | \"size-48\" | \"size-64\" | \"size-72\" | \"size-80\" | \"size-112\" | \"size-2\"; top?: \"0\" | \"size-4\" | \"size-8\" | \"size-12\" | \"size-16\" | \"size-20\" | \"size-24\" | \"size-32\" | \"size-40\" | \"size-48\" | \"size-64\" | \"size-72\" | \"size-80\" | \"size-112\" | \"size-2\"; bottom?: \"0\" | \"size-4\" | \"size-8\" | \"size-12\" | \"size-16\" | \"size-20\" | \"size-24\" | \"size-32\" | \"size-40\" | \"size-48\" | \"size-64\" | \"size-72\" | \"size-80\" | \"size-112\" | \"size-2\"; x?: \"0\" | \"size-4\" | \"size-8\" | \"size-12\" | \"size-16\" | \"size-20\" | \"size-24\" | \"size-32\" | \"size-40\" | \"size-48\" | \"size-64\" | \"size-72\" | \"size-80\" | \"size-112\" | \"size-2\"; y?: \"0\" | \"size-4\" | \"size-8\" | \"size-12\" | \"size-16\" | \"size-20\" | \"size-24\" | \"size-32\" | \"size-40\" | \"size-48\" | \"size-64\" | \"size-72\" | \"size-80\" | \"size-112\" | \"size-2\"; }",
        "references": {
          "ResponsiveType": {
            "location": "import",
            "path": "../../types/ui.type"
          },
          "PaddingType": {
            "location": "import",
            "path": "../../types/ui.type"
          }
        }
      },
      "required": false,
      "optional": false,
      "docs": {
        "tags": [{
            "name": "vegaVersion",
            "text": "2.59.0"
          }],
        "text": "Determines the spacing between the content and the edges of the popover content box."
      },
      "attribute": "padding",
      "reflect": false,
      "defaultValue": "'size-16'"
    }
  }; }
  static get states() { return {
    "isPopoverShow": {}
  }; }
  static get events() { return [{
      "method": "vegaPopoverShow",
      "name": "vegaPopoverShow",
      "bubbles": true,
      "cancelable": true,
      "composed": true,
      "docs": {
        "tags": [{
            "name": "vegaVersion",
            "text": "1.22.0"
          }],
        "text": "An event emitter notifying the popover show event."
      },
      "complexType": {
        "original": "string",
        "resolved": "string",
        "references": {}
      }
    }, {
      "method": "popoverShow",
      "name": "popoverShow",
      "bubbles": true,
      "cancelable": true,
      "composed": true,
      "docs": {
        "tags": [{
            "name": "eventSemantics",
            "text": "namespace:native"
          }, {
            "name": "vegaVersion",
            "text": "2.0.0"
          }],
        "text": "An event emitter notifying the popover show event."
      },
      "complexType": {
        "original": "string",
        "resolved": "string",
        "references": {}
      }
    }, {
      "method": "vegaPopoverHide",
      "name": "vegaPopoverHide",
      "bubbles": true,
      "cancelable": true,
      "composed": true,
      "docs": {
        "tags": [{
            "name": "vegaVersion",
            "text": "1.22.0"
          }],
        "text": "An event emitter notifying the popover hide event."
      },
      "complexType": {
        "original": "string",
        "resolved": "string",
        "references": {}
      }
    }, {
      "method": "popoverHide",
      "name": "popoverHide",
      "bubbles": true,
      "cancelable": true,
      "composed": true,
      "docs": {
        "tags": [{
            "name": "eventSemantics",
            "text": "namespace:native"
          }, {
            "name": "vegaVersion",
            "text": "2.0.0"
          }],
        "text": "An event emitter notifying the popover hide event."
      },
      "complexType": {
        "original": "string",
        "resolved": "string",
        "references": {}
      }
    }]; }
  static get methods() { return {
    "show": {
      "complexType": {
        "signature": "(refresh?: boolean) => Promise<void>",
        "parameters": [{
            "tags": [{
                "name": "param",
                "text": "refresh - Determines whether the popover should be refreshed even when it is already visible. This is useful for updating the popover\u2018s styles (such as width, height, or position). The default value is false. Added in 2.73.0."
              }],
            "text": "- Determines whether the popover should be refreshed even when it is already visible. This is useful for updating the popover\u2018s styles (such as width, height, or position). The default value is false. Added in 2.73.0."
          }],
        "references": {
          "Promise": {
            "location": "global"
          }
        },
        "return": "Promise<void>"
      },
      "docs": {
        "text": "Invoke this method to show the popover.",
        "tags": [{
            "name": "vegaVersion",
            "text": "1.22.0"
          }, {
            "name": "param",
            "text": "refresh - Determines whether the popover should be refreshed even when it is already visible. This is useful for updating the popover\u2018s styles (such as width, height, or position). The default value is false. Added in 2.73.0."
          }]
      }
    },
    "hide": {
      "complexType": {
        "signature": "() => Promise<void>",
        "parameters": [],
        "references": {
          "Promise": {
            "location": "global"
          }
        },
        "return": "Promise<void>"
      },
      "docs": {
        "text": "Invoke this method to close the popover.",
        "tags": [{
            "name": "vegaVersion",
            "text": "1.22.0"
          }]
      }
    }
  }; }
  static get elementRef() { return "host"; }
  static get watchers() { return [{
      "propName": "isPopoverShow",
      "methodName": "watchIsPopoverShow"
    }, {
      "propName": "size",
      "methodName": "watchToRefreshPopover"
    }, {
      "propName": "placement",
      "methodName": "watchToRefreshPopover"
    }, {
      "propName": "alignment",
      "methodName": "watchToRefreshPopover"
    }]; }
}
__decorate([
  InjectVegaGlobalSlimmer()
], VegaPopover.prototype, "globalSlimmers", void 0);
__decorate([
  InjectVegaSlimmer()
], VegaPopover.prototype, "appenderRenderer", void 0);
__decorate([
  InjectVegaSlimmer()
], VegaPopover.prototype, "appenderController", void 0);
__decorate([
  InjectVegaSlimmer()
], VegaPopover.prototype, "popoverShowEventEmitter", void 0);
__decorate([
  InjectVegaSlimmer()
], VegaPopover.prototype, "popoverHideEventEmitter", void 0);
__decorate([
  InjectVegaSlimmer()
], VegaPopover.prototype, "vegaComponentUsageRuntimeMetricsSlimmer", void 0);
__decorate([
  InjectKeyboardManager((host) => [
    {
      keyCode: 'Escape',
      activeElementInScope: [host.host],
      callback: host.hidePopover,
    },
    {
      keyCode: 'Escape',
      activeElementInScope: [host.appenderRenderer.getPopoverContentBox()],
      callback: host.hidePopover,
      eventName: 'keydown',
      preventDefault: true,
    },
    {
      keyCode: 'Tab',
      activeElementOutScope: [host.host, host.appenderRenderer.getPopoverContentBox()],
      callback: host.hidePopover,
    },
    {
      keyCode: 'Tab',
      activeElementInScope: [host.host],
      /**
       * The below method is e2e-test covered in
       * @see{module:vega-popover-keyboard-control-hover-mode-e2e}
       */
      callback: /* istanbul ignore next */ () => {
        if (host.trigger === 'hover') {
          host.showPopover();
        }
      },
    },
  ])
], VegaPopover.prototype, "host", void 0);
