import { shallowEqual as i } from "../../../../../@react-dnd_shallowequal@4.0.2/node_modules/@react-dnd/shallowequal/dist/index.js";
import { isRef as n } from "./isRef.js";
import { wrapConnectorHooks as s } from "./wrapConnectorHooks.js";
class h {
  receiveHandlerId(r) {
    this.handlerId !== r && (this.handlerId = r, this.reconnect());
  }
  get connectTarget() {
    return this.dragSource;
  }
  get dragSourceOptions() {
    return this.dragSourceOptionsInternal;
  }
  set dragSourceOptions(r) {
    this.dragSourceOptionsInternal = r;
  }
  get dragPreviewOptions() {
    return this.dragPreviewOptionsInternal;
  }
  set dragPreviewOptions(r) {
    this.dragPreviewOptionsInternal = r;
  }
  reconnect() {
    const r = this.reconnectDragSource();
    this.reconnectDragPreview(r);
  }
  reconnectDragSource() {
    const r = this.dragSource, e = this.didHandlerIdChange() || this.didConnectedDragSourceChange() || this.didDragSourceOptionsChange();
    return e && this.disconnectDragSource(), this.handlerId ? r ? (e && (this.lastConnectedHandlerId = this.handlerId, this.lastConnectedDragSource = r, this.lastConnectedDragSourceOptions = this.dragSourceOptions, this.dragSourceUnsubscribe = this.backend.connectDragSource(this.handlerId, r, this.dragSourceOptions)), e) : (this.lastConnectedDragSource = r, e) : e;
  }
  reconnectDragPreview(r = !1) {
    const e = this.dragPreview, t = r || this.didHandlerIdChange() || this.didConnectedDragPreviewChange() || this.didDragPreviewOptionsChange();
    if (t && this.disconnectDragPreview(), !!this.handlerId) {
      if (!e) {
        this.lastConnectedDragPreview = e;
        return;
      }
      t && (this.lastConnectedHandlerId = this.handlerId, this.lastConnectedDragPreview = e, this.lastConnectedDragPreviewOptions = this.dragPreviewOptions, this.dragPreviewUnsubscribe = this.backend.connectDragPreview(this.handlerId, e, this.dragPreviewOptions));
    }
  }
  didHandlerIdChange() {
    return this.lastConnectedHandlerId !== this.handlerId;
  }
  didConnectedDragSourceChange() {
    return this.lastConnectedDragSource !== this.dragSource;
  }
  didConnectedDragPreviewChange() {
    return this.lastConnectedDragPreview !== this.dragPreview;
  }
  didDragSourceOptionsChange() {
    return !i(this.lastConnectedDragSourceOptions, this.dragSourceOptions);
  }
  didDragPreviewOptionsChange() {
    return !i(this.lastConnectedDragPreviewOptions, this.dragPreviewOptions);
  }
  disconnectDragSource() {
    this.dragSourceUnsubscribe && (this.dragSourceUnsubscribe(), this.dragSourceUnsubscribe = void 0);
  }
  disconnectDragPreview() {
    this.dragPreviewUnsubscribe && (this.dragPreviewUnsubscribe(), this.dragPreviewUnsubscribe = void 0, this.dragPreviewNode = null, this.dragPreviewRef = null);
  }
  get dragSource() {
    return this.dragSourceNode || this.dragSourceRef && this.dragSourceRef.current;
  }
  get dragPreview() {
    return this.dragPreviewNode || this.dragPreviewRef && this.dragPreviewRef.current;
  }
  clearDragSource() {
    this.dragSourceNode = null, this.dragSourceRef = null;
  }
  clearDragPreview() {
    this.dragPreviewNode = null, this.dragPreviewRef = null;
  }
  constructor(r) {
    this.hooks = s({
      dragSource: (e, t) => {
        this.clearDragSource(), this.dragSourceOptions = t || null, n(e) ? this.dragSourceRef = e : this.dragSourceNode = e, this.reconnectDragSource();
      },
      dragPreview: (e, t) => {
        this.clearDragPreview(), this.dragPreviewOptions = t || null, n(e) ? this.dragPreviewRef = e : this.dragPreviewNode = e, this.reconnectDragPreview();
      }
    }), this.handlerId = null, this.dragSourceRef = null, this.dragSourceOptionsInternal = null, this.dragPreviewRef = null, this.dragPreviewOptionsInternal = null, this.lastConnectedHandlerId = null, this.lastConnectedDragSource = null, this.lastConnectedDragSourceOptions = null, this.lastConnectedDragPreview = null, this.lastConnectedDragPreviewOptions = null, this.backend = r;
  }
}
export {
  h as SourceConnector
};
//# sourceMappingURL=SourceConnector.js.map
