import { GraphQLError, GraphQLScalarType, Kind } from 'graphql';
import { TIMELESS_DATE_REGEX } from '../../../shared/constants.js';
import { dateToTimelessDateString } from '../../../shared/helpers/index.js';
function validateTimelessDateFormat(raw) {
    if (TIMELESS_DATE_REGEX.test(raw)) {
        return raw;
    }
    throw new GraphQLError(`Expected TimelessDate to be of yyyy-mm-dd format, but got: "${raw}"`);
}
export const TimelessDateScalar = new GraphQLScalarType({
    name: 'TimelessDate',
    description: 'A date with no time of day',
    serialize(value) {
        if (typeof value === 'string') {
            return validateTimelessDateFormat(value);
        }
        if (value instanceof Date) {
            return dateToTimelessDateString(value);
        }
        throw new GraphQLError(`Expected TimelessDate to be yyyy-mm-dd string, but got: "${value}"`);
    },
    parseValue(value) {
        if (typeof value === 'string') {
            return validateTimelessDateFormat(value);
        }
        if (value instanceof Date) {
            return dateToTimelessDateString(value);
        }
        throw new GraphQLError(`Expected TimelessDate to be yyyy-mm-dd string, but got: "${value}"`);
    },
    parseLiteral(ast) {
        if (ast.kind === Kind.STRING) {
            return validateTimelessDateFormat(ast.value);
        }
        throw new GraphQLError(`Can only validate strings as TimelessDate but got: ${ast.kind}`);
    },
});
//# sourceMappingURL=timeless-date.js.map