"use strict";
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;
};
Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
const jsxRuntime = require("react/jsx-runtime");
const reactBinding = require("@contember/react-binding");
const react = require("react");
const parseIframeSrc = require("../../../utils/parseIframeSrc.cjs");
const parseUrl = require("../../../utils/parseUrl.cjs");
class SoundCloudEmbedHandler {
  constructor(options) {
    __publicField(this, "debugName", "SoundCloud");
    __publicField(this, "discriminateBy");
    this.options = options;
    this.discriminateBy = options.discriminateBy;
  }
  staticRender() {
    return /* @__PURE__ */ jsxRuntime.jsx(reactBinding.SugaredField, { field: this.options.soundCloudIdField });
  }
  handleSource(source, url) {
    if (url) {
      return void 0;
    }
    const iframeSrc = parseIframeSrc.parseIframeSrc(source);
    const iframeUrl = iframeSrc ? parseUrl.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__ */ jsxRuntime.jsx(SoundCloudEmbedHandler.Renderer, { soundCloudIdField: this.options.soundCloudIdField });
  }
  populateEmbedData({ entity, embedArtifacts }) {
    entity.getField(this.options.soundCloudIdField).updateValue(embedArtifacts);
  }
}
((SoundCloudEmbedHandler2) => {
  SoundCloudEmbedHandler2.Renderer = react.memo(function SoundCloudRenderer(props) {
    const soundCloudId = reactBinding.useField(props.soundCloudIdField).value;
    if (soundCloudId === null) {
      return null;
    }
    return /* @__PURE__ */ jsxRuntime.jsx(
      "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 = {}));
exports.SoundCloudEmbedHandler = SoundCloudEmbedHandler;
//# sourceMappingURL=SoundCloudEmbedHandler.cjs.map
