/**
 * Generated by a build script. Do not modify.
 * Check orginal .jsx file.
 */
/* eslint-disable */

((wp2, Drupal2) => {
  const { element, components } = wp2;
  const { Button } = components;
  const { Component } = element;
  const __ = Drupal2.t;
  class MediaLibrary extends Component {
    constructor(args) {
      super(args);
      this.openDialog = this.openDialog.bind(this);
      this.closeDialog = this.closeDialog.bind(this);
    }
    closeDialog(callback) {
      const { onDialogClose } = this.props;
      if (this.frame) {
        this.frame.close();
        delete this.frame;
      }
      if (this.mediaBrowserWrapper) {
        Drupal2.detachBehaviors(this.mediaBrowserWrapper);
        this.mediaBrowserWrapper.remove();
        delete this.mediaBrowserWrapper;
      }
      if (onDialogClose)
        onDialogClose();
      if (callback)
        callback();
    }
    openDialog() {
      const {
        allowedTypes = [],
        allowedBundles = [],
        onDialogInsert,
        onDialogCreate,
        onClose = () => {
        },
        getDialog,
        multiple
      } = this.props;
      getDialog({
        allowedTypes,
        allowedBundles,
        onSelect: () => this.closeDialog(onClose)
      }).then((result) => {
        this.mediaBrowserWrapper = document.createElement("div");
        this.mediaBrowserWrapper.setAttribute(
          "id",
          "media-entity-browser-modal"
        );
        element.render(
          /* @__PURE__ */ React.createElement(result.component, { ...this.props }),
          this.mediaBrowserWrapper,
          () => {
            this.frame = Drupal2.dialog(this.mediaBrowserWrapper, {
              title: __("Media library"),
              width: "95%",
              height: document.documentElement.clientHeight - 100,
              buttons: {
                [__("Insert")]: () => {
                  if (onDialogInsert)
                    onDialogInsert(this.mediaBrowserWrapper, this.props);
                  this.closeDialog(onClose);
                },
                [__("Cancel")]: () => this.closeDialog(onClose)
              },
              create: (event) => onDialogCreate(event.target, multiple)
              // close: () => { onClose(); /*this.closeDialog();*/ },
            });
            if (this.frame)
              this.frame.showModal();
            if (this.mediaBrowserWrapper)
              Drupal2.attachBehaviors(this.mediaBrowserWrapper);
          }
        );
      }).catch((reason) => console.warn("reason", reason));
    }
    render() {
      const { render } = this.props;
      if (render) {
        return render({ open: this.openDialog });
      }
      return /* @__PURE__ */ React.createElement(
        Button,
        {
          isLarge: true,
          isSecondary: true,
          title: __("Media Library"),
          onClick: this.openDialog
        },
        __("Media Library")
      );
    }
  }
  window.DrupalGutenberg = window.DrupalGutenberg || {};
  window.DrupalGutenberg.Components = window.DrupalGutenberg.Components || {};
  window.DrupalGutenberg.Components.MediaLibrary = MediaLibrary;
})(wp, Drupal, DrupalGutenberg, jQuery, drupalSettings);
