function ProductLinkDecorator(props) {
  const { entityKey, contentState, children } = props;
  const data = contentState.getEntity(entityKey).getData();

  return window.React.createElement(
    "a",
    {
      href: data.url,
      title: data.title || data.url,
      target: "_blank",
      rel: "noopener noreferrer",
      style: {
        textDecoration: "underline",
        color: "#2980b9",
      },
    },
    children
  );
}


class ProductModalWorkflowSource extends window.draftail.ModalWorkflowSource {
    getChooserConfig() {
      // Get the chooser URL from either entityType or global
      let productChooserUrl;

      if (this.props.entityType && this.props.entityType.chooserUrls) {
        productChooserUrl = this.props.entityType.chooserUrls.productChooser;
      } else if (window.chooserUrls) {
        productChooserUrl = window.chooserUrls.productChooser;
      }

      return {
        url: productChooserUrl,
        urlParams: {},
        onload: window.DOCUMENT_CHOOSER_MODAL_ONLOAD_HANDLERS,
        responses: {
          chosen: this.onChosen,
        },
      };
    }

    filterEntityData(data) {
      return {
        url: data.url,
        id: data.id,
        title: data.title,
      };
    }
}


window.draftail.registerPlugin({
  type: "PRODUCT",
  source: ProductModalWorkflowSource,
  decorator: ProductLinkDecorator,
}, "entityTypes");
