import JSDOMEnvironment from "jest-environment-jsdom";
import { cloneDeep } from "lodash";

// https://github.com/facebook/jest/blob/v29.4.3/website/versioned_docs/version-29.4/Configuration.md#testenvironment-string
export default class FixJSDOMEnvironment extends JSDOMEnvironment {
  constructor(...args: ConstructorParameters<typeof JSDOMEnvironment>) {
    super(...args);

    // TODO Tests aren't polyfilled.
    this.global.structuredClone = cloneDeep;
    // FIXME https://github.com/nodejs/node/issues/35889
    // Add missing importActual() function to mirror requireActual(),
    // which lets us work around the ESM bug.
    // Wrap the construction of the function in eval, so that transpilers
    // don't touch the import() call.
    this.global.importActual = eval("url => import(url)");

    /**
     * By default, Jest only passes a limited number of global objects to the test environment. We need these global
     * objects when testing code that loads save data.
     */
    this.global.Uint8Array = Uint8Array;
    this.global.Blob = Blob;
    this.global.CompressionStream = CompressionStream;
    this.global.DecompressionStream = DecompressionStream;
    this.global.TextDecoderStream = TextDecoderStream;
    this.global.URL = URL;
    this.global.Response = Response;

    /**
     * https://github.com/jsdom/jsdom/issues/3766
     * https://github.com/jsdom/jsdom/issues/3444
     */
    this.global.document.adoptedStyleSheets = [];
    this.global.CSSStyleSheet.prototype.replaceSync = () => {};
  }
}
