"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.Hex = void 0;
var Vector2_1 = require("./Vector2");
var Cube = /** @class */ (function () {
    function Cube(x, y, z) {
        this.x = x;
        this.y = y;
        this.z = z;
    }
    return Cube;
}());
var Hex = /** @class */ (function () {
    function Hex(x, y) {
        this.col = x;
        this.row = y;
    }
    Hex.prototype.toWorld = function () {
        return Hex.toWorld(this);
    };
    Hex.prototype.toPixel = function (size) {
        return Hex.toPixel(this, size);
    };
    Hex.offsetDirections = [
        [
            new Hex(+1, 0),
            new Hex(0, -1),
            new Hex(-1, -1),
            new Hex(-1, 0),
            new Hex(-1, +1),
            new Hex(0, +1)
        ],
        [
            new Hex(+1, 0),
            new Hex(+1, -1),
            new Hex(0, -1),
            new Hex(-1, 0),
            new Hex(0, +1),
            new Hex(+1, +1),
            new Hex(+1, +1)
        ]
    ];
    Hex.neighbour = function (hex, direction) {
        var parity = hex.row & 1;
        var dir = Hex.offsetDirections[parity][direction];
        return new Hex(hex.col + dir.col, hex.row + dir.row);
    };
    Hex.distance = function (a, b) {
        var ac = Hex.offsetToCube(a);
        var bc = Hex.offsetToCube(b);
        return Math.max(Math.abs(ac.x - bc.x), Math.abs(ac.y - bc.y), Math.abs(ac.z - bc.z));
    };
    Hex.offsetToCube = function (hex) {
        var x = hex.col - (hex.row - (hex.row & 1)) / 2;
        var z = hex.row;
        var y = -x - z;
        return new Cube(x, y, z);
    };
    Hex.toWorld = function (hex) {
        var x = Math.sqrt(3) * (hex.col + 0.5 * (hex.row & 1));
        var y = (3 / 2) * hex.row;
        return new Vector2_1.Vector2(x, y);
    };
    Hex.fromWorld = function (coord) {
        var q = (coord.x * Math.sqrt(3)) / 3 - coord.y / 3;
        var r = (coord.y * 2) / 3;
        var cube = new Cube(q, -q - r, r);
        var rx = Math.round(cube.x);
        var ry = Math.round(cube.y);
        var rz = Math.round(cube.z);
        var xDiff = Math.abs(rx - cube.x);
        var yDiff = Math.abs(ry - cube.y);
        var zDiff = Math.abs(rz - cube.z);
        if (xDiff > yDiff && xDiff > zDiff) {
            rx = -ry - rz;
        }
        else if (yDiff > zDiff) {
            ry = -rx - rz;
        }
        else {
            rz = -rx - ry;
        }
        var rounded = new Cube(rx, ry, rz);
        var col = rounded.x + (rounded.z - (rounded.z & 1)) / 2;
        var row = rounded.z;
        return new Hex(col, row);
    };
    Hex.toPixel = function (hex, size) {
        var w = Hex.toWorld(hex);
        return new Vector2_1.Vector2(w.x * size, w.y * size);
    };
    Hex.fromPixel = function (coord, size) {
        return Hex.fromWorld(new Vector2_1.Vector2(coord.x / size, coord.y / size));
    };
    return Hex;
}());
exports.Hex = Hex;
