import { FieldContext } from '@vinejs/vine/types';
import vine from '@vinejs/vine';
import { VineString } from '@vinejs/vine';

async function isValidMimetype(value: unknown, options: unknown, field: FieldContext) {
    if (typeof value !== 'string') {
        return;
    }

    // Regex pattern to match valid mimetypes (e.g., "application/json", "text/html")
    const mimetypePattern = /^[a-zA-Z0-9!#$&^_.+-]+\/[a-zA-Z0-9!#$&^_.+-]+$/;
    if (!mimetypePattern.test(value)) {
        field.report('The given value is not a valid mimetype', 'isValidMimetype', field);
    }
}

export const isValidMimetypeRule = vine.createRule(isValidMimetype);

declare module '@vinejs/vine' {
    interface VineString {
        isValidMimetype(): this;
    }
}

VineString.macro('isValidMimetype', function (this: VineString) {
    return this.use(isValidMimetypeRule());
});


