<commit_before>import {
  KeyValue,
  generateUID,
  EMPTY_ARRAY,
  TreeNodeUpdater
} from "tandem-common";
import { TreeNode, Bounds } from "tandem-common";
import { DependencyGraph } from "./graph";
import {
  getPCNode,
  PCVisibleNode,
  getPCNodeFrame,
  getPCNodeDependency
} from "./dsl";
import { PaperclipRoot } from "state";

export type ComputedDisplayInfo = {
  [identifier: string]: {
    bounds: Bounds;
    style: CSSStyleDeclaration;
  };
};

export type SyntheticFrames = KeyValue<SyntheticFrame>;

export type SyntheticSource = {
  nodeId: string;
};

export type SyntheticFrame = {
  id: string;
  root: SyntheticNode;
  source: SyntheticSource;

  // internal only
  $container: HTMLIFrameElement;
  computed?: ComputedDisplayInfo;
};

export type SyntheticBaseNode = {
  source: SyntheticSource;
} & TreeNode<string>;

export type SyntheticElement = {
  attributes: KeyValue<string>;
  style: KeyValue<any>;
} & SyntheticBaseNode;

export type SyntheticTextNode = {
  value: string;
  style: KeyValue<any>;
} & SyntheticBaseNode;

export type SyntheticNode = SyntheticElement | SyntheticTextNode;

export const createSyntheticElement = (
  name: string,
  source: SyntheticSource,
  style: KeyValue<any> = {},
  attributes: KeyValue<string>,
  children: SyntheticNode[] = []
): SyntheticElement => ({
  id: generateUID(),
  source,
  name,
  attributes,
  style,
  children
});

export const createSyntheticTextNode = (
  value: string,
  source: SyntheticSource,
  style: KeyValue<any> = {}
): SyntheticTextNode => ({
  id: generateUID(),
  value,
  source,
  name,
  style,
  children: EMPTY_ARRAY
});

export const getSyntheticSourceNode = (
  node: SyntheticNode,
  graph: DependencyGraph
) => getPCNode(node.source.nodeId, graph) as PCVisibleNode;
export const getSyntheticSourceFrame = (
  node: SyntheticNode,
  graph: DependencyGraph
) =>
  getPCNodeFrame(
    node.source.nodeId,
    getPCNodeDependency(node.source.nodeId, graph).content
  );
export const isSyntheticNodeRoot = (
  node: SyntheticNode,
  graph: DependencyGraph
) => getSyntheticSourceFrame(node, graph).children[0].id === node.source.nodeId;

export const mergeSyntheticFrames = (
  oldFrames: SyntheticFrames,
  newFrames: SyntheticFrames
) => {
  const updatedFrames: SyntheticFrames = {};
  for (const sourceFrameId in newFrames) {
    const newFrame = newFrames[sourceFrameId];
    const oldFrame = oldFrames[sourceFrameId];
    if (!oldFrame || oldFrame === newFrame) {
      continue;
    }

    const ots = []; //
    const patchedRoot = newFrame.root;

    updatedFrames[sourceFrameId] = {
      ...oldFrame,
      root: patchedRoot,
      computed: newFrame.computed
    };
  }
};

export const persistSyntheticNodeChanges = (
  node: SyntheticNode,
  state: PaperclipRoot,
  updater: TreeNodeUpdater<SyntheticNode>
) => {<commit_msg>return frames<commit_after>import {
  KeyValue,
  generateUID,
  EMPTY_ARRAY,
  TreeNodeUpdater
} from "tandem-common";
import { TreeNode, Bounds } from "tandem-common";
import { DependencyGraph } from "./graph";
import {
  getPCNode,
  PCVisibleNode,
  getPCNodeFrame,
  getPCNodeDependency
} from "./dsl";
import { PaperclipRoot } from "state";

export type ComputedDisplayInfo = {
  [identifier: string]: {
    bounds: Bounds;
    style: CSSStyleDeclaration;
  };
};

export type SyntheticFrames = KeyValue<SyntheticFrame>;

export type SyntheticSource = {
  nodeId: string;
};

export type SyntheticFrame = {
  id: string;
  root: SyntheticNode;
  source: SyntheticSource;

  // internal only
  $container: HTMLIFrameElement;
  computed?: ComputedDisplayInfo;
};

export type SyntheticBaseNode = {
  source: SyntheticSource;
} & TreeNode<string>;

export type SyntheticElement = {
  attributes: KeyValue<string>;
  style: KeyValue<any>;
} & SyntheticBaseNode;

export type SyntheticTextNode = {
  value: string;
  style: KeyValue<any>;
} & SyntheticBaseNode;

export type SyntheticNode = SyntheticElement | SyntheticTextNode;

export const createSyntheticElement = (
  name: string,
  source: SyntheticSource,
  style: KeyValue<any> = {},
  attributes: KeyValue<string>,
  children: SyntheticNode[] = []
): SyntheticElement => ({
  id: generateUID(),
  source,
  name,
  attributes,
  style,
  children
});

export const createSyntheticTextNode = (
  value: string,
  source: SyntheticSource,
  style: KeyValue<any> = {}
): SyntheticTextNode => ({
  id: generateUID(),
  value,
  source,
  name,
  style,
  children: EMPTY_ARRAY
});

export const getSyntheticSourceNode = (
  node: SyntheticNode,
  graph: DependencyGraph
) => getPCNode(node.source.nodeId, graph) as PCVisibleNode;
export const getSyntheticSourceFrame = (
  node: SyntheticNode,
  graph: DependencyGraph
) =>
  getPCNodeFrame(
    node.source.nodeId,
    getPCNodeDependency(node.source.nodeId, graph).content
  );
export const isSyntheticNodeRoot = (
  node: SyntheticNode,
  graph: DependencyGraph
) => getSyntheticSourceFrame(node, graph).children[0].id === node.source.nodeId;

export const mergeSyntheticFrames = (
  oldFrames: SyntheticFrames,
  newFrames: SyntheticFrames
) => {
  const updatedFrames: SyntheticFrames = {};
  for (const sourceFrameId in newFrames) {
    const newFrame = newFrames[sourceFrameId];
    const oldFrame = oldFrames[sourceFrameId];
    if (!oldFrame || oldFrame === newFrame) {
      continue;
    }

    const ots = []; //
    const patchedRoot = newFrame.root;

    updatedFrames[sourceFrameId] = {
      ...oldFrame,
      root: patchedRoot,
      computed: newFrame.computed
    };
  }

  return updatedFrames;
};

export const persistSyntheticNodeChanges = (
  node: SyntheticNode,
  state: PaperclipRoot,
  updater: TreeNodeUpdater<SyntheticNode>
) => {
{'fraction_non_alphanumeric': 0.11231820571228317, 'fraction_numerical': 0.0003504468196951113, 'mean_word_length': 4.349578256794752, 'pattern_counts': {'":': 0, '<': 21, '<?xml version=': 0, '>': 35, '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': '7570831', 'n_tokens_mistral': 1912, 'n_tokens_neox': 1801, 'n_words': 411}