import { Direction } from "../constants/Direction";
import { TrackType } from "../constants/TrackType";
import { Cell } from "./Cell";
import { ExternalNode } from "./ExternalNode";
import { InternalNode } from "./InternalNode";

export class PlacedPiece {
  tracks: Set<{
    nodes: {
      firstNode: ExternalNode;
      secondNode: ExternalNode | InternalNode;
    };
    type: TrackType;
  }>;
  internalNode?: InternalNode;
  cell: Cell;
  constructor(
    tracks: Set<{
      nodes: {
        firstNode: ExternalNode;
        secondNode: ExternalNode | InternalNode;
      };
      type: TrackType;
    }>,
    internalNodes: InternalNode | undefined,
    cell: Cell,
  ) {
    this.tracks = tracks;
    this.internalNode = internalNodes;
    this.cell = cell;
  }

  public findTrackForDirection(direction: Direction) {
    return Array.from(this.tracks).find(
      (track) =>
        track.nodes.firstNode.direction === direction ||
        (track.nodes.secondNode instanceof ExternalNode &&
          track.nodes.secondNode.direction === direction),
    );
  }
}
