"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.serializeColor = exports.isSerializedColor = exports.isColor = exports.getRandomColor = exports.deserializeColor = exports.copyColor = exports.colorEquals = void 0;
const SerializationBrand_1 = require("../enums/private/SerializationBrand");
const isaacAPIClass_1 = require("./isaacAPIClass");
const random_1 = require("./random");
const rng_1 = require("./rng");
const table_1 = require("./table");
const types_1 = require("./types");
const utils_1 = require("./utils");
const OBJECT_NAME = "Color";
const KEYS = ["R", "G", "B", "A", "RO", "GO", "BO"];
function colorEquals(color1, color2) {
    return (0, isaacAPIClass_1.isaacAPIClassEquals)(color1, color2, KEYS);
}
exports.colorEquals = colorEquals;
/** Helper function to copy a `Color` Isaac API class. */
function copyColor(color) {
    if (!isColor(color)) {
        error(`Failed to copy a ${OBJECT_NAME} object since the provided object was not a userdata ${OBJECT_NAME} class.`);
    }
    return Color(color.R, color.G, color.B, color.A, color.RO, color.GO, color.BO);
}
exports.copyColor = copyColor;
/**
 * Helper function to convert a `SerializedColor` object to a normal `Color` object. (This is used
 * by the save data manager when reading data from the "save#.dat" file.)
 */
function deserializeColor(color) {
    if (!(0, types_1.isTable)(color)) {
        error(`Failed to deserialize a ${OBJECT_NAME} object since the provided object was not a Lua table.`);
    }
    const [r, g, b, a, ro, go, bo] = (0, table_1.getNumbersFromTable)(color, OBJECT_NAME, ...KEYS);
    (0, utils_1.assertDefined)(r, `Failed to deserialize a ${OBJECT_NAME} object since the provided object did not have a value for: R`);
    (0, utils_1.assertDefined)(g, `Failed to deserialize a ${OBJECT_NAME} object since the provided object did not have a value for: G`);
    (0, utils_1.assertDefined)(b, `Failed to deserialize a ${OBJECT_NAME} object since the provided object did not have a value for: B`);
    return Color(r, g, b, a, ro, go, bo);
}
exports.deserializeColor = deserializeColor;
/**
 * Helper function to get a random `Color` object.
 *
 * If you want to generate an unseeded object, you must explicitly pass `undefined` to the
 * `seedOrRNG` parameter.
 *
 * @param seedOrRNG The `Seed` or `RNG` object to use. If an `RNG` object is provided, the
 *                  `RNG.Next` method will be called. If `undefined` is provided, it will default to
 *                  a random seed.
 * @param alpha Optional. The alpha value to use. Default is 1.
 */
function getRandomColor(seedOrRNG, alpha = 1) {
    const rng = (0, rng_1.isRNG)(seedOrRNG) ? seedOrRNG : (0, rng_1.newRNG)(seedOrRNG);
    const r = (0, random_1.getRandom)(rng);
    const g = (0, random_1.getRandom)(rng);
    const b = (0, random_1.getRandom)(rng);
    return Color(r, g, b, alpha);
}
exports.getRandomColor = getRandomColor;
/** Helper function to check if something is an instantiated `Color` object. */
function isColor(object) {
    return (0, isaacAPIClass_1.isIsaacAPIClassOfType)(object, OBJECT_NAME);
}
exports.isColor = isColor;
/**
 * Used to determine is the given table is a serialized `Color` object created by the `deepCopy`
 * function.
 */
function isSerializedColor(object) {
    if (!(0, types_1.isTable)(object)) {
        return false;
    }
    return (0, table_1.tableHasKeys)(object, ...KEYS) && object.has(SerializationBrand_1.SerializationBrand.COLOR);
}
exports.isSerializedColor = isSerializedColor;
/**
 * Helper function to convert a `Color` object to a `SerializedColor` object. (This is used by the
 * save data manager when writing data from the "save#.dat" file.)
 */
function serializeColor(color) {
    if (!isColor(color)) {
        error(`Failed to serialize a ${OBJECT_NAME} object since the provided object was not a userdata ${OBJECT_NAME} class.`);
    }
    const colorTable = new LuaMap();
    (0, table_1.copyUserdataValuesToTable)(color, KEYS, colorTable);
    colorTable.set(SerializationBrand_1.SerializationBrand.COLOR, "");
    return colorTable;
}
exports.serializeColor = serializeColor;
