export var cardinalDirectionTypes;
(function (cardinalDirectionTypes) {
  cardinalDirectionTypes["North"] = "north";
  cardinalDirectionTypes["South"] = "south";
  cardinalDirectionTypes["East"] = "east";
  cardinalDirectionTypes["West"] = "west";
})(cardinalDirectionTypes || (cardinalDirectionTypes = {}));
export var multiblockPositions;
(function (multiblockPositions) {
  multiblockPositions["Single"] = "single";
  multiblockPositions["Left"] = "left";
  multiblockPositions["Right"] = "right";
})(multiblockPositions || (multiblockPositions = {}));

export class multiblockManager {
  //update block, runs every 5 ticks, set in "tick" component within the block.json
  static update(block) {
    // Check if a player is nearby before updating (optional performance optimization)
    const closest = block.dimension.getEntities({
      maxDistance: 30,
      type: "minecraft:player",
    });
    if (closest == undefined) return;

    const facingDirection = block.permutation.getState(
      "minecraft:cardinal_direction"
    );
    const currentPosition = block.permutation.getState("pb_dino:mb_position");

    // If the block is currently "Single"
    if (currentPosition === multiblockPositions.Single) {
      // Check for adjacent blocks to see if it should become part of a multi-block structure
      const adjacentBlocks = [
        this.getAdjacentBlock(block, facingDirection, "left"),
        this.getAdjacentBlock(block, facingDirection, "right"),
      ];

      for (const adjacentBlock of adjacentBlocks) {
        if (
          adjacentBlock &&
          adjacentBlock.permutation.getState("pb_dino:mb_position") ===
            multiblockPositions.Single &&
          this.isBlockAandB(block, adjacentBlock)
        ) {
          // Update both blocks to form the multi-block structure
          this.setMultiBlock(block, adjacentBlock);
          break;
        }
      }
    } else {
      // The block is part of a multi-block structure (Left or Right)
      const adjacentPosition =
        currentPosition === multiblockPositions.Left ? "right" : "left";
      const adjacentBlock = this.getAdjacentBlock(
        block,
        facingDirection,
        adjacentPosition
      );

      // Check if the structure is still valid; if not, reset to "Single"
      if (
        !adjacentBlock ||
        !this.isBlockAandB(block, adjacentBlock) ||
        adjacentBlock.permutation.getState("pb_dino:mb_position") ===
          multiblockPositions.Single
      ) {
        block.setPermutation(
          block.permutation.withState(
            "pb_dino:mb_position",
            multiblockPositions.Single
          )
        );
        if (adjacentBlock) {
          try {
            adjacentBlock.setPermutation(
              adjacentBlock.permutation.withState(
                "pb_dino:mb_position",
                multiblockPositions.Single
              )
            );
          } catch (error) {}
        }
      }
    }
  }

  static getAdjacentBlock(block, direction, side) {
    try {
      switch (side) {
        case "left":
          switch (direction) {
            case cardinalDirectionTypes.North:
              return block.west(1);
            case cardinalDirectionTypes.South:
              return block.east(1);
            case cardinalDirectionTypes.East:
              return block.north(1);
            case cardinalDirectionTypes.West:
              return block.south(1);
          }
          break;
        case "right":
          switch (direction) {
            case cardinalDirectionTypes.North:
              return block.east(1);
            case cardinalDirectionTypes.South:
              return block.west(1);
            case cardinalDirectionTypes.East:
              return block.south(1);
            case cardinalDirectionTypes.West:
              return block.north(1);
          }
          break;
      }
    } catch {
      return undefined;
    }
  }

  static isBlockAandB(blockA, blockB) {
    const typeIdA = "pb_dino:pyrolyzer"; // Block A
    const typeIdB = "pb_dino:pyrolyzer_tank"; // Block B

    // Ensure one is Block A and the other is Block B
    return (
      (blockA.typeId == typeIdA && blockB.typeId == typeIdB) ||
      (blockA.typeId == typeIdB && blockB.typeId == typeIdA)
    );
  }

  static setMultiBlock(blockA, blockB) {
    // Get the direction that blockA is facing
    const facingDirectionA = blockA.permutation.getState("minecraft:cardinal_direction");
    const facingDirectionB = blockB.permutation.getState("minecraft:cardinal_direction");
  
    // Only proceed if both blocks are facing the same direction
    if (facingDirectionA !== facingDirectionB) {
      return; // Exit if directions don't match
    }
  
    // Determine if blockB is to the "right" or "left" of blockA based on facing direction
    const adjacentRight = this.getAdjacentBlock(blockA, facingDirectionA, "right");
    const adjacentLeft = this.getAdjacentBlock(blockA, facingDirectionA, "left");
  
    let mainBlock = null;
    let secondaryBlock = null;
  
    if (adjacentRight && adjacentRight.typeId === blockB.typeId) {
      // If blockB is to the right of blockA
      blockA.setPermutation(
        blockA.permutation.withState("pb_dino:mb_position", multiblockPositions.Left)
      );
      blockB.setPermutation(
        blockB.permutation.withState("pb_dino:mb_position", multiblockPositions.Right)
      );
      mainBlock = blockA;
      secondaryBlock = blockB;
    } else if (adjacentLeft && adjacentLeft.typeId === blockB.typeId) {
      // If blockB is to the left of blockA
      blockA.setPermutation(
        blockA.permutation.withState("pb_dino:mb_position", multiblockPositions.Right)
      );
      blockB.setPermutation(
        blockB.permutation.withState("pb_dino:mb_position", multiblockPositions.Left)
      );
      mainBlock = blockA;
      secondaryBlock = blockB;
    }
  
    // If mainBlock and secondaryBlock are defined, check and sync oilQty if necessary
    if (mainBlock && secondaryBlock) {
      try {
        const oilQtyA = mainBlock.permutation.getState("pb_dino:oil_quantity");
        const oilQtyB = secondaryBlock.permutation.getState("pb_dino:oil_quantity");
  
        // Sync the oil quantity from whichever block has it
        if (oilQtyA !== undefined) {
          secondaryBlock.setPermutation(
            secondaryBlock.permutation.withState("pb_dino:oil_quantity", oilQtyA)
          );
        } else if (oilQtyB !== undefined) {
          mainBlock.setPermutation(
            mainBlock.permutation.withState("pb_dino:oil_quantity", oilQtyB)
          );
        }
      } catch (error) {
        console.error("Error syncing oil quantity:", error);
      }
    }
  }
  
  
  
}
