"use strict";
/* eslint-disable @typescript-eslint/no-empty-interface */
Object.defineProperty(exports, "__esModule", { value: true });
exports.defineIndexingRuleModels = exports.IndexingRule = exports.INDEXING_RULE_GLOBAL = exports.IndexingDecisionBasis = void 0;
const sequelize_1 = require("sequelize");
const ethers_1 = require("ethers");
var IndexingDecisionBasis;
(function (IndexingDecisionBasis) {
    IndexingDecisionBasis["RULES"] = "rules";
    IndexingDecisionBasis["NEVER"] = "never";
    IndexingDecisionBasis["ALWAYS"] = "always";
})(IndexingDecisionBasis = exports.IndexingDecisionBasis || (exports.IndexingDecisionBasis = {}));
exports.INDEXING_RULE_GLOBAL = 'global';
class IndexingRule extends sequelize_1.Model {
    // eslint-disable-next-line @typescript-eslint/ban-types
    toGraphQL() {
        return Object.assign(Object.assign({}, this.toJSON()), { __typename: 'IndexingRule' });
    }
    // eslint-disable-next-line @typescript-eslint/ban-types
    mergeToGraphQL(global) {
        if (global instanceof IndexingRule) {
            // eslint-disable-next-line @typescript-eslint/no-explicit-any
            const globalRule = global.toJSON();
            // eslint-disable-next-line @typescript-eslint/no-explicit-any
            const rule = this.toJSON();
            for (const k in globalRule) {
                if (null == rule[k]) {
                    rule[k] = globalRule[k];
                }
            }
            for (const k in rule) {
                if (rule[k] == undefined) {
                    rule[k] = globalRule[k];
                }
            }
            return Object.assign(Object.assign({}, rule), { __typename: 'IndexingRule' });
        }
        else {
            return this.toGraphQL();
        }
    }
}
exports.IndexingRule = IndexingRule;
exports.defineIndexingRuleModels = (sequelize) => {
    IndexingRule.init({
        id: {
            type: sequelize_1.DataTypes.INTEGER,
            autoIncrement: true,
            unique: true,
        },
        deployment: {
            type: sequelize_1.DataTypes.STRING,
            allowNull: true,
            primaryKey: true,
            validate: {
                // eslint-disable-next-line @typescript-eslint/no-explicit-any
                isDeploymentID: (value) => {
                    if (typeof value !== 'string') {
                        throw new Error('Deployment ID must be a string');
                    }
                    // "global" is ok
                    if (value === 'global') {
                        return;
                    }
                    // "0x..." is ok
                    if (ethers_1.utils.isHexString(value, 32)) {
                        return;
                    }
                    throw new Error(`Deployment ID must be "global" or a valid subgraph deployment ID`);
                },
            },
        },
        allocationAmount: {
            type: sequelize_1.DataTypes.DECIMAL,
            allowNull: true,
            validate: {
                min: 0.0,
            },
        },
        parallelAllocations: {
            type: sequelize_1.DataTypes.INTEGER,
            allowNull: true,
            validate: {
                min: 0,
                max: 20,
                emitDeprecationWarning: (value) => {
                    if (value > 1) {
                        throw new Error('Parallel allocations are soon to be fully deprecated. Please set parallel allocations to 1 for all your indexing rules');
                    }
                },
            },
        },
        maxAllocationPercentage: {
            type: sequelize_1.DataTypes.FLOAT,
            allowNull: true,
            validate: {
                isFloat: true,
                min: 0.0,
                max: 1.0,
            },
        },
        minSignal: {
            type: sequelize_1.DataTypes.DECIMAL,
            allowNull: true,
            validate: {
                min: 0.0,
            },
        },
        minStake: {
            type: sequelize_1.DataTypes.DECIMAL,
            allowNull: true,
            validate: {
                min: 0.0,
            },
        },
        maxSignal: {
            type: sequelize_1.DataTypes.DECIMAL,
            allowNull: true,
            validate: {
                min: 0.0,
            },
        },
        minAverageQueryFees: {
            type: sequelize_1.DataTypes.DECIMAL,
            allowNull: true,
            validate: {
                min: 0.0,
            },
        },
        custom: {
            type: sequelize_1.DataTypes.STRING,
            allowNull: true,
        },
        decisionBasis: {
            type: sequelize_1.DataTypes.ENUM('rules', 'never', 'always'),
            allowNull: false,
            defaultValue: 'rules',
        },
    }, {
        modelName: 'IndexingRule',
        sequelize,
    });
    return {
        ['IndexingRule']: IndexingRule,
    };
};
//# sourceMappingURL=indexing-rule.js.map