"use strict";
/* eslint-disable @typescript-eslint/no-empty-interface */
Object.defineProperty(exports, "__esModule", { value: true });
exports.defineCostModelModels = exports.CostModel = exports.parseGraphQLCostModel = void 0;
const sequelize_1 = require("sequelize");
const ethers_1 = require("ethers");
exports.parseGraphQLCostModel = (costModel) => {
    try {
        const variables = !costModel.variables
            ? costModel.variables
            : JSON.parse(costModel.variables);
        return {
            deployment: costModel.deployment,
            model: costModel.model || null,
            variables: variables,
        };
    }
    catch (error) {
        throw new Error(`Failed to parse GraphQL cost model: ${error}`);
    }
};
class CostModel extends sequelize_1.Model {
    // eslint-disable-next-line @typescript-eslint/ban-types
    toGraphQL() {
        return Object.assign(Object.assign({}, this.toJSON()), { variables: this.variables === null ? this.variables : JSON.stringify(this.variables), __typename: 'CostModel' });
    }
}
exports.CostModel = CostModel;
exports.defineCostModelModels = (sequelize) => {
    CostModel.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');
                    }
                    // "0x..." is ok
                    if (ethers_1.utils.isHexString(value, 32)) {
                        return;
                    }
                    throw new Error(`Deployment ID must be a valid subgraph deployment ID`);
                },
            },
        },
        model: {
            type: sequelize_1.DataTypes.TEXT,
            allowNull: true,
        },
        variables: {
            type: sequelize_1.DataTypes.JSONB,
            allowNull: true,
            validate: {
                isObject: (value) => {
                    if (value === null || value === undefined) {
                        return;
                    }
                    if (value instanceof Object && !(value instanceof Array)) {
                        return;
                    }
                    throw new Error(`Variables must be a valid object or null`);
                },
            },
        },
    }, {
        modelName: 'CostModel',
        sequelize,
    });
    return { ['CostModel']: CostModel };
};
//# sourceMappingURL=cost-model.js.map