<commit_before>import { throwNullException } from "./NullException";
import { StringBuilder } from "./StringBuilder";
import { ListDefinition } from "./ListDefinition";
import { Story } from "./Story";

export class InkListItem implements IInkListItem {
  // InkListItem is a struct

  public readonly originName: string | null = null;
  public readonly itemName: string | null = null;

  constructor(originName: string | null, itemName: string | null);
  constructor(fullName: string | null);
  constructor() {
    if (typeof arguments[1] !== "undefined") {
      let originName = arguments[0] as string | null;
      let itemName = arguments[1] as string | null;

      this.originName = originName;
      this.itemName = itemName;
    } else if (arguments[0]) {
      let fullName = arguments[0] as string;

      let nameParts = fullName.toString().split(".");
      this.originName = nameParts[0];
      this.itemName = nameParts[1];
    }
  }
  public static get Null() {
    return new InkListItem(null, null);
  }
  public get isNull() {
    return this.originName == null && this.itemName == null;
  }
  get fullName() {
    return (
      (this.originName !== null ? this.originName : "?") + "." + this.itemName
    );
  }
  public toString(): string {
    return this.fullName;
  }
  public Equals(obj: InkListItem) {
    if (obj instanceof InkListItem) {
      let otherItem = obj;
      return (
        otherItem.itemName == this.itemName &&
        otherItem.originName == this.originName
      );
    }

    return false;
  }

  // These methods did not exist in the original C# code. Their purpose is to
  // make `InkListItem` mimics the value-type semantics of the original
  // struct. Please refer to the end of this file, for a more in-depth
  // explanation.

  /**
   * Returns a shallow clone of the current instance.
   */
  public copy() {
    return new InkListItem(this.originName, this.itemName);
  }
  /**
   * Returns a `SerializedInkListItem` representing the current
   * instance. The result is intended to be used as a key inside a Map.
   */
  public serialized(): SerializedInkListItem {
    // We are simply using a JSON representation as a value-typed key.
    return JSON.stringify({
      originName: this.originName,
      itemName: this.itemName,
    });
  }

  /**
   * Reconstructs a `InkListItem` from the given SerializedInkListItem.
   */
  public static fromSerializedKey(key: SerializedInkListItem): InkListItem {
    let obj = JSON.parse(key);
    if (!InkListItem.isLikeInkListItem(obj)) return InkListItem.Null;

    let inkListItem = obj as IInkListItem;

    return new InkListItem(inkListItem.originName, inkListItem.itemName);
  }

  /**
   * Determines whether the given item is sufficiently `InkListItem`-like
   * to be used as a template when reconstructing the InkListItem.
   */
  private static isLikeInkListItem(item: any) {
    if (typeof item !== "object") return false;
    if (!item.hasOwnProperty("originName") || !item.hasOwnProperty("itemName"))
      return false;
    if (typeof item.originName !== "string" && typeof item.originName !== null)
      return false;
    if (typeof item.itemName !== "string" && typeof item.itemName !== null)
      return false;

    return true;
  }
}

export class InkList extends Map<SerializedInkListItem, number> {
  public origins: ListDefinition[] | null = null;
  public _originNames: string[] | null = [];

  constructor();
  constructor(otherList: InkList);
  constructor(singleOriginListName: string, originStory: Story);
  constructor(singleElement: KeyValuePair<InkListItem, number>);
  constructor() {
    // Trying to be smart here, this emulates the constructor inheritance found
    // in the original code, but only if otherList is an InkList. IIFE FTW.
    super(
      (() => {
        if (arguments[0] instanceof InkList) {
          return arguments[0];
        } else {
          return [];
        }
      })()
    );

    if (arguments[0] instanceof InkList) {
      let otherList = arguments[0] as InkList;

      var otherOriginNames = otherList.originNames;
      if (otherOriginNames !== null)
        this._originNames = otherOriginNames.slice();
      if (otherList.origins !== null) {
        this.origins = otherList.origins.slice();
      }
    } else if (typeof arguments[0] === "string") {
      let singleOriginListName = arguments[0] as string;
      let originStory = arguments[1] as Story;
      this.SetInitialOriginName(singleOriginListName);

      if (originStory.listDefinitions === null) {
        return throwNullException("originStory.listDefinitions");
      }
      let def = originStory.listDefinitions.TryListGetDefinition(
        singleOriginListName,
        null
      );
      if (def.exists) {
        // Throwing now, because if the value is `null` it will
        // eventually throw down the line.
        if (def.result === null) {<commit_msg>missing type<commit_after>import { throwNullException } from "./NullException";
import { StringBuilder } from "./StringBuilder";
import { ListDefinition } from "./ListDefinition";
import { Story } from "./Story";

export class InkListItem implements IInkListItem {
  // InkListItem is a struct

  public readonly originName: string | null = null;
  public readonly itemName: string | null = null;

  constructor(originName: string | null, itemName: string | null);
  constructor(fullName: string | null);
  constructor() {
    if (typeof arguments[1] !== "undefined") {
      let originName = arguments[0] as string | null;
      let itemName = arguments[1] as string | null;

      this.originName = originName;
      this.itemName = itemName;
    } else if (arguments[0]) {
      let fullName = arguments[0] as string;

      let nameParts = fullName.toString().split(".");
      this.originName = nameParts[0];
      this.itemName = nameParts[1];
    }
  }
  public static get Null() {
    return new InkListItem(null, null);
  }
  public get isNull() {
    return this.originName == null && this.itemName == null;
  }
  get fullName() {
    return (
      (this.originName !== null ? this.originName : "?") + "." + this.itemName
    );
  }
  public toString(): string {
    return this.fullName;
  }
  public Equals(obj: InkListItem) {
    if (obj instanceof InkListItem) {
      let otherItem = obj;
      return (
        otherItem.itemName == this.itemName &&
        otherItem.originName == this.originName
      );
    }

    return false;
  }

  // These methods did not exist in the original C# code. Their purpose is to
  // make `InkListItem` mimics the value-type semantics of the original
  // struct. Please refer to the end of this file, for a more in-depth
  // explanation.

  /**
   * Returns a shallow clone of the current instance.
   */
  public copy() {
    return new InkListItem(this.originName, this.itemName);
  }
  /**
   * Returns a `SerializedInkListItem` representing the current
   * instance. The result is intended to be used as a key inside a Map.
   */
  public serialized(): SerializedInkListItem {
    // We are simply using a JSON representation as a value-typed key.
    return JSON.stringify({
      originName: this.originName,
      itemName: this.itemName,
    });
  }

  /**
   * Reconstructs a `InkListItem` from the given SerializedInkListItem.
   */
  public static fromSerializedKey(key: SerializedInkListItem): InkListItem {
    let obj = JSON.parse(key);
    if (!InkListItem.isLikeInkListItem(obj)) return InkListItem.Null;

    let inkListItem = obj as IInkListItem;

    return new InkListItem(inkListItem.originName, inkListItem.itemName);
  }

  /**
   * Determines whether the given item is sufficiently `InkListItem`-like
   * to be used as a template when reconstructing the InkListItem.
   */
  private static isLikeInkListItem(item: any) {
    if (typeof item !== "object") return false;
    if (!item.hasOwnProperty("originName") || !item.hasOwnProperty("itemName"))
      return false;
    if (typeof item.originName !== "string" && typeof item.originName !== null)
      return false;
    if (typeof item.itemName !== "string" && typeof item.itemName !== null)
      return false;

    return true;
  }
}

export class InkList extends Map<SerializedInkListItem, number> {
  public origins: ListDefinition[] | null = null;
  public _originNames: string[] | null = [];

  constructor();
  constructor(otherList: InkList);
  constructor(singleOriginListName: string, originStory: Story);
  constructor(singleElement: KeyValuePair<InkListItem, number>);
  constructor() {
    // Trying to be smart here, this emulates the constructor inheritance found
    // in the original code, but only if otherList is an InkList. IIFE FTW.
    super(
      (() => {
        if (arguments[0] instanceof InkList) {
          return arguments[0];
        } else {
          return [];
        }
      })()
    );

    if (arguments[0] instanceof InkList) {
      let otherList = arguments[0] as InkList;

      let otherOriginNames = otherList.originNames as string[];
      if (otherOriginNames !== null)
        this._originNames = otherOriginNames.slice();
      if (otherList.origins !== null) {
        this.origins = otherList.origins.slice();
      }
    } else if (typeof arguments[0] === "string") {
      let singleOriginListName = arguments[0] as string;
      let originStory = arguments[1] as Story;
      this.SetInitialOriginName(singleOriginListName);

      if (originStory.listDefinitions === null) {
        return throwNullException("originStory.listDefinitions");
      }
      let def = originStory.listDefinitions.TryListGetDefinition(
        singleOriginListName,
        null
      );
      if (def.exists) {
        // Throwing now, because if the value is `null` it will
        // eventually throw down the line.
        if (def.result === null) {
{'fraction_non_alphanumeric': 0.11146140547137191, 'fraction_numerical': 0.0028475541543781144, 'mean_word_length': 2.974939369442199, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 9, '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': '7448422', 'n_tokens_mistral': 2890, 'n_tokens_neox': 2828, 'n_words': 855}