import Zod from "../Utils/Schema/Zod";
import DatabaseProperty from "./Database/DatabaseProperty";
import BadDataException from "./Exception/BadDataException";
import { ObjectType } from "./JSON";
export default class Color extends DatabaseProperty {
    get color() {
        return this._color;
    }
    set color(v) {
        this._color = v;
    }
    constructor(color) {
        super();
        this.color = color.toString();
    }
    toString() {
        return this.color;
    }
    toJSON() {
        return {
            _type: ObjectType.Color,
            value: this.toString(),
        };
    }
    static fromJSON(json) {
        if (json["_type"] === ObjectType.Color) {
            return new Color(json["value"] || "");
        }
        throw new BadDataException("Invalid JSON: " + JSON.stringify(json));
    }
    static toDatabase(value) {
        if (value) {
            if (typeof value === "string") {
                value = new Color(value);
            }
            return value.toString();
        }
        return null;
    }
    static fromDatabase(_value) {
        if (_value) {
            return new Color(_value);
        }
        return null;
    }
    static colorToRgb(color) {
        const re = /^#?([a-f\d]{2})([a-f\d]{2})([a-f\d]{2})$/i;
        const result = re.exec(color.toString());
        if (!result) {
            throw new BadDataException("Invalid color: " + color.toString());
        }
        return {
            red: parseInt(result[1] ? result[1] : "0", 16),
            green: parseInt(result[2] ? result[2] : "0", 16),
            blue: parseInt(result[3] ? result[3] : "0", 16),
        };
    }
    static _componentToHex(c) {
        const hex = c.toString(16);
        return hex.length === 1 ? "0" + hex : hex;
    }
    static rgbToColor(rgb) {
        return new Color("#" +
            this._componentToHex(rgb.red) +
            this._componentToHex(rgb.green) +
            this._componentToHex(rgb.blue));
    }
    static fromString(color) {
        return new Color(color);
    }
    static shouldUseDarkText(color) {
        const rgb = Color.colorToRgb(color);
        if (rgb.red * 0.299 + rgb.green * 0.587 + rgb.blue * 0.114 > 186) {
            return true;
        }
        return false;
    }
    static getSchema() {
        return Zod.object({
            _type: Zod.literal(ObjectType.Color),
            value: Zod.string().openapi({
                type: "string",
                example: "#ff0000",
            }),
        }).openapi({
            type: "object",
            description: "Color object",
            example: { _type: ObjectType.Color, value: "#ff0000" },
        });
    }
}
//# sourceMappingURL=Color.js.map