import { property } from 'lit/decorators.js';
import { boolean } from '../../internal/boolean.mjs';
import FlowUiIconButton from '../icon-button/icon-button.component.mjs';
import styles from './split-view-toggle-button.styles.mjs';

var __defProp = Object.defineProperty;
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
var __decorateClass = (decorators, target, key, kind) => {
  var result = kind > 1 ? void 0 : kind ? __getOwnPropDesc(target, key) : target;
  for (var i = decorators.length - 1, decorator; i >= 0; i--)
    if (decorator = decorators[i])
      result = (kind ? decorator(target, key, result) : decorator(result)) || result;
  if (kind && result)
    __defProp(target, key, result);
  return result;
};
class FlowUiSplitViewToggleButton extends FlowUiIconButton {
  constructor() {
    super(...arguments);
    this.parentPane = null;
    this.parentPanelObserver = null;
    this.name = "chevron-right";
    this.library = "system";
    this.closed = false;
  }
  connectedCallback() {
    super.connectedCallback();
    this.closed = boolean(this.closed, true);
    this.parentPane = this.closestElement("flow-ui-split-view-pane");
    if (this.parentPane) {
      this.parentPanelObserver = new MutationObserver(() => {
        this.closed = this.parentPane.closed;
      });
      this.parentPanelObserver.observe(this.parentPane, {
        attributes: true,
        attributeFilter: ["closed"]
      });
      this.closed = this.parentPane.closed;
    }
  }
  disconnectedCallback() {
    super.disconnectedCallback();
    if (this.parentPanelObserver) {
      this.parentPanelObserver.disconnect();
      this.parentPanelObserver = null;
    }
  }
  toggle(emitEvents = true) {
    this.closed = !this.closed;
    if (emitEvents) {
      this.emit(this.closed ? "flow-ui-collapse" : "flow-ui-expand");
    }
  }
  handleClick(event) {
    super.handleClick(event);
    this.toggle();
  }
  closestElement(selector, base = this) {
    function __closestFrom(el) {
      if (!el || el === document || el === window)
        return null;
      if (el.matches(selector))
        return el;
      if (el.assignedSlot)
        el = el.assignedSlot;
      const found = el.closest(selector);
      return found ? found : __closestFrom(el.getRootNode().host);
    }
    return __closestFrom(base);
  }
}
FlowUiSplitViewToggleButton.styles = [FlowUiIconButton.styles, styles];
__decorateClass([
  property()
], FlowUiSplitViewToggleButton.prototype, "name", 2);
__decorateClass([
  property()
], FlowUiSplitViewToggleButton.prototype, "library", 2);
__decorateClass([
  property({ type: Boolean, reflect: true })
], FlowUiSplitViewToggleButton.prototype, "closed", 2);

export { FlowUiSplitViewToggleButton as default };
