// This is for Object ID for all the things in our database.
import UUID from "../Utils/UUID";
import DatabaseProperty from "./Database/DatabaseProperty";
import BadDataException from "./Exception/BadDataException";
import { ObjectType } from "./JSON";
import Zod from "../Utils/Schema/Zod";
// UUID validation regex - matches standard UUID format
const UUID_REGEX = /^[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}$/i;
export default class ObjectID extends DatabaseProperty {
    get id() {
        return this._id;
    }
    set id(v) {
        this._id = v;
    }
    constructor(id) {
        var _a;
        super();
        this._id = "";
        if (id instanceof ObjectID) {
            this.id = id.toString();
        }
        else if (typeof id === "string") {
            this.id = id;
        }
        else if (typeof id === "object" &&
            id &&
            id["_type"] === ObjectType.ObjectID &&
            id["value"]) {
            this.id = ((_a = id["value"]) === null || _a === void 0 ? void 0 : _a.toString()) || "";
        }
    }
    get value() {
        return this._id.toString();
    }
    equals(other) {
        var _a, _b;
        return (((_a = this.id) === null || _a === void 0 ? void 0 : _a.toString()) || "") === (((_b = other.id) === null || _b === void 0 ? void 0 : _b.toString()) || "");
    }
    toString() {
        return this.id;
    }
    static generate() {
        return new this(UUID.generate());
    }
    /**
     * Time-ordered id (RFC 9562 UUIDv7: 48-bit unix-ms prefix + random).
     * Use for analytics-row `_id`s — time-ordered ids cluster ClickHouse
     * inserts and compress dramatically better than random v4 UUIDs.
     * Postgres entity ids keep using `generate()` (random v4).
     */
    static generateTimeOrdered() {
        return new this(UUID.generateTimeOrdered());
    }
    static toJSONArray(ids) {
        if (!ids || ids.length === 0) {
            return [];
        }
        return ids.map((id) => {
            if (typeof id === "string") {
                id = new ObjectID(id);
            }
            return id.toJSON();
        });
    }
    static toDatabase(value) {
        if (value) {
            if (typeof value === "string") {
                value = new ObjectID(value);
            }
            return value.toString();
        }
        return null;
    }
    toJSON() {
        return {
            _type: ObjectType.ObjectID,
            value: this.toString(),
        };
    }
    static fromJSON(json) {
        if (json["_type"] === ObjectType.ObjectID) {
            return new ObjectID(json["value"] || "");
        }
        throw new BadDataException("Invalid JSON: " + JSON.stringify(json));
    }
    static fromJSONArray(json) {
        return json.map((value) => {
            return ObjectID.fromJSON(value);
        });
    }
    static fromDatabase(_value) {
        if (_value) {
            return new ObjectID(_value);
        }
        return null;
    }
    static getZeroObjectID() {
        return new ObjectID("00000000-0000-0000-0000-000000000000");
    }
    static fromString(id) {
        return new ObjectID(id);
    }
    /**
     * Check if a string is a valid UUID format
     */
    static isValidUUID(id) {
        if (!id) {
            return false;
        }
        return UUID_REGEX.test(id.toString());
    }
    /**
     * Validate that a string is a valid UUID, throw BadDataException if not
     */
    static validateUUID(id) {
        if (!ObjectID.isValidUUID(id.toString())) {
            throw new BadDataException(`Invalid ID format: "${id}". Expected a valid UUID (e.g., "550e8400-e29b-41d4-a716-446655440000").`);
        }
    }
    static getSchema() {
        return Zod.string().openapi({
            type: "string",
            example: "123e4567-e89b-12d3-a456-426614174000",
            format: "uuid",
            description: "A unique identifier for an object, represented as a UUID.",
        });
    }
}
//# sourceMappingURL=ObjectID.js.map