import { frontmatterSchema, metaSchema } from 'fumadocs-mdx/config';
import { preprocess, string, array } from '../node_modules/.pnpm/zod@4.1.12/node_modules/zod/v4/classic/schemas.mjs';

// Reusable schema for date
const createDateSchema = () => preprocess((arg) => {
    if (arg instanceof Date) {
        // Format Date object to YYYY-MM-DD string
        const year = arg.getFullYear();
        const month = (arg.getMonth() + 1).toString().padStart(2, '0');
        const day = arg.getDate().toString().padStart(2, '0');
        return `${year}-${month}-${day}`;
    }
    if (typeof arg === 'string') {
        return arg.trim();
    }
    // For other types or null/undefined, let the subsequent string validation handle it
    return arg;
}, string()
    .regex(/^\d{4}-\d{2}-\d{2}$/, "Date must be in YYYY-MM-DD format or a valid Date object")
    .refine((val) => !isNaN(new Date(val).getTime()), 'Invalid date!'));
// common docs frontmatter  schema
// @ts-ignore
const createCommonDocsSchema = () => frontmatterSchema
    .extend({
    date: createDateSchema(),
    author: string().optional(),
    keywords: array(string()).optional(),
});
// common meta schema
// @ts-ignore
const createCommonMetaSchema = () => metaSchema.extend({});
const remarkInstallOptions = {
    persist: {
        id: 'package-manager',
    },
};

export { createCommonDocsSchema, createCommonMetaSchema, createDateSchema, remarkInstallOptions };
