"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.Result = void 0;
const constants_1 = require("@scrabble-solver/constants");
const Cell_1 = require("./Cell");
class Result {
    constructor({ cells, id, collisions, points, }) {
        const tiles = getTiles(cells);
        this.blanksCount = getBlanks(tiles).length;
        this.cells = cells;
        this.collisions = collisions;
        this.consonantsCount = getConsonants(tiles).length;
        this.id = id;
        this.length = cells.length;
        this.points = points;
        this.pointsRatio = getPointsRatio(tiles, points);
        this.tiles = tiles;
        this.tilesCount = tiles.length;
        this.vowelsCount = getVowels(tiles).length;
        this.word = getWord(cells);
        this.words = getWords(cells, collisions);
        this.wordsCount = 1 + this.collisions.length;
    }
    toJson() {
        return {
            cells: this.cells.map((cell) => cell.toJson()),
            id: this.id,
            collisions: this.collisions.map((collision) => collision.map((cell) => cell.toJson())),
            points: this.points,
        };
    }
    isHorizontal() {
        return this.cells[0].y === this.cells[1].y;
    }
    isVertical() {
        return this.cells[0].x === this.cells[1].x;
    }
}
exports.Result = Result;
Result.fromJson = (json) => {
    return new Result({
        id: json.id,
        cells: json.cells.map(Cell_1.Cell.fromJson),
        collisions: json.collisions.map((collision) => collision.map(Cell_1.Cell.fromJson)),
        points: json.points,
    });
};
const getBlanks = (tiles) => tiles.filter(({ isBlank }) => isBlank);
const getConsonants = (tiles) => tiles.filter(isConsonant);
const getVowels = (tiles) => tiles.filter(isVowel);
const getPointsRatio = (tiles, points) => points / tiles.length;
const getTiles = (cells) => cells.filter(({ isEmpty }) => isEmpty).map(({ tile }) => tile);
const getWord = (cells) => cells.reduce((word, cell) => word + cell.toString(), '');
const getWords = (cells, collisions) => [cells, ...collisions].map(getWord);
const isConsonant = ({ character, isBlank }) => constants_1.CONSONANTS.includes(character) && !isBlank;
const isVowel = ({ character, isBlank }) => constants_1.VOWELS.includes(character) && !isBlank;
