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 { jsxs, Fragment, jsx } from "react/jsx-runtime";
import { SugaredField, useField } from "@contember/react-binding";
import { memo } from "react";
import { parseIframeSrc } from "../../../utils/parseIframeSrc.js";
import { parseUrl } from "../../../utils/parseUrl.js";
class SpotifyEmbedHandler {
  constructor(options) {
    __publicField(this, "debugName", "Spotify");
    __publicField(this, "discriminateBy");
    this.options = options;
    this.discriminateBy = options.discriminateBy;
  }
  staticRender() {
    return /* @__PURE__ */ jsxs(Fragment, { children: [
      /* @__PURE__ */ jsx(SugaredField, { field: this.options.spotifyTypeField }),
      /* @__PURE__ */ jsx(SugaredField, { field: this.options.spotifyIdField })
    ] });
  }
  handleSource(source, url) {
    if (!url) {
      source = parseIframeSrc(source) ?? source;
      if (source.startsWith("open.spotify.com")) {
        source = `https://${source}`;
      }
      url = parseUrl(source);
      if (!url) {
        return void 0;
      }
    }
    if (!url.host.endsWith("open.spotify.com")) {
      return void 0;
    }
    const matches = url.pathname.match(/^\/(embed\/)?(.+)\/(.+)$/);
    if (!matches) {
      return void 0;
    }
    return {
      type: matches[2],
      id: matches[3]
    };
  }
  renderEmbed() {
    if (this.options.render) {
      return this.options.render();
    }
    return /* @__PURE__ */ jsx(
      SpotifyEmbedHandler.Renderer,
      {
        spotifyTypeField: this.options.spotifyTypeField,
        spotifyIdField: this.options.spotifyIdField
      }
    );
  }
  populateEmbedData({ entity, embedArtifacts }) {
    entity.getField(this.options.spotifyTypeField).updateValue(embedArtifacts.type);
    entity.getField(this.options.spotifyIdField).updateValue(embedArtifacts.id);
  }
}
((SpotifyEmbedHandler2) => {
  SpotifyEmbedHandler2.Renderer = memo(function SpotifyRenderer(props) {
    const spotifyType = useField(props.spotifyTypeField).value;
    const spotifyId = useField(props.spotifyIdField).value;
    if (spotifyType === null || spotifyId === null) {
      return null;
    }
    return /* @__PURE__ */ jsx(
      "iframe",
      {
        src: `https://open.spotify.com/embed/${spotifyType}/${spotifyId}`,
        width: "100%",
        height: "232",
        frameBorder: "0",
        allow: "encrypted-media"
      }
    );
  });
})(SpotifyEmbedHandler || (SpotifyEmbedHandler = {}));
export {
  SpotifyEmbedHandler
};
//# sourceMappingURL=SpotifyEmbedHandler.js.map
