"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.UniqueConstraintError = void 0;
exports.isUniqueConstraintError = isUniqueConstraintError;
exports.normalizeDatabaseError = normalizeDatabaseError;
const tslib_1 = require("tslib");
const _a = tslib_1.__importStar(require("@zyno-io/ts-reflection/type-metadata-runtime"));
const __tsf_metadata_type = _a.createCompactMetadataRegistryV1("[1,[{\"kind\":2}]]", []);
let _UniqueConstraintErrorMetadata_1;
class UniqueConstraintError extends Error {
    cause;
    constructor(message = 'Unique constraint violation', cause) {
        super(message);
        this.cause = cause;
        this.name = 'UniqueConstraintError';
    }
    static __tsfType = _UniqueConstraintErrorMetadata_1 = _a.decodeCompactMetadataV1("[1,{\"kind\":16,\"name\":\"UniqueConstraintError\",\"typeName\":\"UniqueConstraintError\",\"classType\":{\"$tsf\":0},\"properties\":[],\"methods\":[],\"hasConstructor\":true,\"constructorParameters\":[{\"name\":\"message\",\"type\":{\"$tsfType\":0},\"optional\":true,\"default\":true},{\"name\":\"cause\",\"type\":{\"$tsfType\":0},\"optional\":true,\"default\":false}]}]", [() => UniqueConstraintError], __tsf_metadata_type);
}
exports.UniqueConstraintError = UniqueConstraintError;
_UniqueConstraintErrorMetadata_1.classType = UniqueConstraintError;
UniqueConstraintError.__tsfType = _UniqueConstraintErrorMetadata_1;
function isUniqueConstraintError(error) {
    if (error instanceof UniqueConstraintError)
        return true;
    if (typeof error !== 'object' || error === null)
        return false;
    const candidate = error;
    return (candidate.name === 'UniqueConstraintFailure' || candidate.code === 'ER_DUP_ENTRY' || candidate.code === '23505' || candidate.errno === 1062);
}
function normalizeDatabaseError(error) {
    if (isUniqueConstraintError(error)) {
        const message = error instanceof Error && error.message ? error.message : 'Unique constraint violation';
        return new UniqueConstraintError(message, error);
    }
    return error;
}
//# sourceMappingURL=errors.js.map