<commit_before>import vegaEmbedV2 from "@nteract/vega-embed-v2";
import vegaEmbedV3 from "@nteract/vega-embed-v3";
import { promisify } from "util";
import vegaEmbedV4 from "vega-embed";
import { MEDIA_TYPES, VegaMediaType } from "./mime";

export interface VegaOptions {
  renderer: "canvas" | "svg";
}

/** Call the external library to do the embedding. */
export async function doEmbedding(
  anchor: HTMLElement,
  mediaType: VegaMediaType,
  spec: Readonly<{}>,
  options: Partial<VegaOptions> = {},
): Promise<any> {
  const version = MEDIA_TYPES[mediaType];
  const defaults = {
    actions: false,
    mode: version.kind,
  };

  if (version.vegaLevel <= 2) {
    await promisify(vegaEmbedV2)(anchor, {...defaults, ...options, spec});
  }
  else if (version.vegaLevel <= 3) {
    await vegaEmbedV3(anchor, deepThaw(spec), {...defaults, ...options});
  }
  else {
    await vegaEmbedV4(anchor, deepThaw(spec), {...defaults, ...options} as {});
  }
}

function deepThaw(spec: Readonly<{}>): {} {
  return JSON.parse(JSON.stringify(spec));
}<commit_msg>Only import vega-embed-v_ when actually needed<commit_after>import { promisify } from "util";
import { MEDIA_TYPES, VegaMediaType } from "./mime";

export interface VegaOptions {
  renderer: "canvas" | "svg";
}

/** Call the external library to do the embedding. */
export async function doEmbedding(
  anchor: HTMLElement,
  mediaType: VegaMediaType,
  spec: Readonly<{}>,
  options: Partial<VegaOptions> = {},
): Promise<any> {
  const version = MEDIA_TYPES[mediaType];
  const defaults = {
    actions: false,
    mode: version.kind,
  };

  if (version.vegaLevel <= 2) {
    return await import("@nteract/vega-embed-v2").then(
      ({ default: vegaEmbed }) =>
        promisify(vegaEmbed)(anchor, { ...defaults, ...options, spec })
    );
  }
  else if (version.vegaLevel <= 3) {
    return await import("@nteract/vega-embed-v3").then(
      ({ default: vegaEmbed }) =>
        vegaEmbed(anchor, deepThaw(spec), {...defaults, ...options})
    );
  }
  else {
    return await import("vega-embed").then(
      ({ default: vegaEmbed }) =>
        vegaEmbed(anchor, deepThaw(spec), {...defaults, ...options})
    );
  }
}

function deepThaw(spec: Readonly<{}>): {} {
  return JSON.parse(JSON.stringify(spec));
}
{'fraction_non_alphanumeric': 0.16512549537648613, 'fraction_numerical': 0.006164685160722149, 'mean_word_length': 3.6845360824742266, 'pattern_counts': {'":': 0, '<': 15, '<?xml version=': 0, '>': 14, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3939579', 'n_tokens_mistral': 798, 'n_tokens_neox': 779, 'n_words': 176}