var __defProp = Object.defineProperty;
var __defNormalProp = (obj, key, value) => key in obj ? __defProp(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value;
var __publicField = (obj, key, value) => {
  __defNormalProp(obj, typeof key !== "symbol" ? key + "" : key, value);
  return value;
};
import { createElement, memo } from "react";
import { SugaredField, useField } from "@contember/binding";
import { parseIframeSrc } from "../../../utils/parseIframeSrc.js";
import { parseUrl } from "../../../utils/parseUrl.js";
class SoundCloudEmbedHandler {
  constructor(options) {
    __publicField(this, "debugName", "SoundCloud");
    __publicField(this, "discriminateBy");
    this.options = options;
    this.discriminateBy = options.discriminateBy;
  }
  staticRender() {
    return /* @__PURE__ */ createElement(SugaredField, {
      field: this.options.soundCloudIdField
    });
  }
  handleSource(source, url) {
    if (url) {
      return void 0;
    }
    const iframeSrc = parseIframeSrc(source);
    const iframeUrl = iframeSrc ? parseUrl(iframeSrc) : void 0;
    if (!iframeUrl) {
      return void 0;
    }
    if (!iframeUrl.host.endsWith("w.soundcloud.com")) {
      return void 0;
    }
    const trackUrl = iframeUrl.searchParams.get("url") || "";
    const matches = trackUrl.match(/^https:\/\/api\.soundcloud\.com\/tracks\/([^\/]*)$/);
    if (!matches) {
      return void 0;
    }
    return matches[1];
  }
  renderEmbed() {
    if (this.options.render) {
      return this.options.render();
    }
    return /* @__PURE__ */ createElement(SoundCloudEmbedHandler.Renderer, {
      soundCloudIdField: this.options.soundCloudIdField
    });
  }
  populateEmbedData({ entity, embedArtifacts }) {
    entity.getField(this.options.soundCloudIdField).updateValue(embedArtifacts);
  }
}
((SoundCloudEmbedHandler2) => {
  SoundCloudEmbedHandler2.Renderer = memo(function SoundCloudRenderer(props) {
    const soundCloudId = useField(props.soundCloudIdField).value;
    if (soundCloudId === null) {
      return null;
    }
    return /* @__PURE__ */ createElement("iframe", {
      width: "100%",
      height: "166",
      scrolling: "no",
      frameBorder: "no",
      allow: "autoplay",
      src: `https://w.soundcloud.com/player/?url=https%3A//api.soundcloud.com/tracks/${soundCloudId}`
    });
  });
})(SoundCloudEmbedHandler || (SoundCloudEmbedHandler = {}));
export { SoundCloudEmbedHandler };
//# sourceMappingURL=SoundCloudEmbedHandler.js.map
