"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.makeSearchableXSortInputObject = exports.makeSearchableXSortableFieldsEnumObject = exports.makeSearchableSortDirectionEnumObject = exports.makeSearchableXFilterInputObject = exports.makeSearchableScalarInputObject = void 0;
const graphql_1 = require("graphql");
const graphql_transformer_common_1 = require("graphql-transformer-common");
const ID_CONDITIONS = [
    'ne',
    'gt',
    'lt',
    'gte',
    'lte',
    'eq',
    'match',
    'matchPhrase',
    'matchPhrasePrefix',
    'multiMatch',
    'exists',
    'wildcard',
    'regexp',
    'range',
];
const STRING_CONDITIONS = ID_CONDITIONS;
const INT_CONDITIONS = ['ne', 'gt', 'lt', 'gte', 'lte', 'eq', 'range'];
const FLOAT_CONDITIONS = ['ne', 'gt', 'lt', 'gte', 'lte', 'eq', 'range'];
const BOOLEAN_CONDITIONS = ['eq', 'ne'];
function makeSearchableScalarInputObject(type) {
    const name = graphql_transformer_common_1.SearchableResourceIDs.SearchableFilterInputTypeName(type);
    const conditions = getScalarConditions(type);
    const fields = conditions.map((condition) => ({
        kind: graphql_1.Kind.INPUT_VALUE_DEFINITION,
        name: { kind: 'Name', value: condition },
        type: getScalarFilterInputType(condition, type, name),
        directives: [],
    }));
    return {
        kind: graphql_1.Kind.INPUT_OBJECT_TYPE_DEFINITION,
        name: {
            kind: 'Name',
            value: name,
        },
        fields,
        directives: [],
    };
}
exports.makeSearchableScalarInputObject = makeSearchableScalarInputObject;
function makeSearchableXFilterInputObject(obj) {
    const name = graphql_transformer_common_1.SearchableResourceIDs.SearchableFilterInputTypeName(obj.name.value);
    const fields = obj.fields
        .filter((field) => (0, graphql_transformer_common_1.isScalar)(field.type) === true)
        .map((field) => ({
        kind: graphql_1.Kind.INPUT_VALUE_DEFINITION,
        name: field.name,
        type: (0, graphql_transformer_common_1.makeNamedType)(graphql_transformer_common_1.SearchableResourceIDs.SearchableFilterInputTypeName((0, graphql_transformer_common_1.getBaseType)(field.type))),
        directives: [],
    }));
    fields.push({
        kind: graphql_1.Kind.INPUT_VALUE_DEFINITION,
        name: {
            kind: 'Name',
            value: 'and',
        },
        type: (0, graphql_transformer_common_1.makeListType)((0, graphql_transformer_common_1.makeNamedType)(name)),
        directives: [],
    }, {
        kind: graphql_1.Kind.INPUT_VALUE_DEFINITION,
        name: {
            kind: 'Name',
            value: 'or',
        },
        type: (0, graphql_transformer_common_1.makeListType)((0, graphql_transformer_common_1.makeNamedType)(name)),
        directives: [],
    }, {
        kind: graphql_1.Kind.INPUT_VALUE_DEFINITION,
        name: {
            kind: 'Name',
            value: 'not',
        },
        type: (0, graphql_transformer_common_1.makeNamedType)(name),
        directives: [],
    });
    return {
        kind: graphql_1.Kind.INPUT_OBJECT_TYPE_DEFINITION,
        name: {
            kind: 'Name',
            value: name,
        },
        fields,
        directives: [],
    };
}
exports.makeSearchableXFilterInputObject = makeSearchableXFilterInputObject;
function makeSearchableSortDirectionEnumObject() {
    const name = (0, graphql_transformer_common_1.graphqlName)(`SearchableSortDirection`);
    return {
        kind: graphql_1.Kind.ENUM_TYPE_DEFINITION,
        name: {
            kind: 'Name',
            value: name,
        },
        values: [
            {
                kind: graphql_1.Kind.ENUM_VALUE_DEFINITION,
                name: { kind: 'Name', value: 'asc' },
                directives: [],
            },
            {
                kind: graphql_1.Kind.ENUM_VALUE_DEFINITION,
                name: { kind: 'Name', value: 'desc' },
                directives: [],
            },
        ],
        directives: [],
    };
}
exports.makeSearchableSortDirectionEnumObject = makeSearchableSortDirectionEnumObject;
function makeSearchableXSortableFieldsEnumObject(obj) {
    const name = (0, graphql_transformer_common_1.graphqlName)(`Searchable${obj.name.value}SortableFields`);
    const values = obj.fields
        .filter((field) => (0, graphql_transformer_common_1.isScalar)(field.type) === true)
        .map((field) => ({
        kind: graphql_1.Kind.ENUM_VALUE_DEFINITION,
        name: field.name,
        directives: [],
    }));
    return {
        kind: graphql_1.Kind.ENUM_TYPE_DEFINITION,
        name: {
            kind: 'Name',
            value: name,
        },
        values,
        directives: [],
    };
}
exports.makeSearchableXSortableFieldsEnumObject = makeSearchableXSortableFieldsEnumObject;
function makeSearchableXSortInputObject(obj) {
    const name = (0, graphql_transformer_common_1.graphqlName)(`Searchable${obj.name.value}SortInput`);
    return {
        kind: graphql_1.Kind.INPUT_OBJECT_TYPE_DEFINITION,
        name: {
            kind: 'Name',
            value: name,
        },
        fields: [
            {
                kind: graphql_1.Kind.INPUT_VALUE_DEFINITION,
                name: { kind: 'Name', value: 'field' },
                type: (0, graphql_transformer_common_1.makeNamedType)(`Searchable${obj.name.value}SortableFields`),
                directives: [],
            },
            {
                kind: graphql_1.Kind.INPUT_VALUE_DEFINITION,
                name: { kind: 'Name', value: 'direction' },
                type: (0, graphql_transformer_common_1.makeNamedType)('SearchableSortDirection'),
                directives: [],
            },
        ],
        directives: [],
    };
}
exports.makeSearchableXSortInputObject = makeSearchableXSortInputObject;
function getScalarFilterInputType(condition, type, filterInputName) {
    switch (condition) {
        case 'range':
            return (0, graphql_transformer_common_1.makeListType)((0, graphql_transformer_common_1.makeNamedType)(type));
        case 'exists':
            return (0, graphql_transformer_common_1.makeNamedType)('Boolean');
        default:
            return (0, graphql_transformer_common_1.makeNamedType)(type);
    }
}
function getScalarConditions(type) {
    switch (type) {
        case 'String':
            return STRING_CONDITIONS;
        case 'ID':
            return ID_CONDITIONS;
        case 'Int':
            return INT_CONDITIONS;
        case 'Float':
            return FLOAT_CONDITIONS;
        case 'Boolean':
            return BOOLEAN_CONDITIONS;
        default:
            throw 'Valid types are String, ID, Int, Float, Boolean';
    }
}
//# sourceMappingURL=definitions.js.map