import { cmsApi } from './api.js';
export async function fetchPostTypeDetail(slug) {
    const res = await cmsApi('/collections');
    if (res.error || !res.data) {
        throw new Error(res.error ?? 'Failed to load collections');
    }
    const row = res.data.find((c) => c.slug === slug);
    if (!row)
        return null;
    return {
        slug: row.slug,
        singular: row.labels?.singular ?? slug,
        plural: row.labels?.plural ?? slug,
        description: row.description ?? null,
        urlPrefix: row.urlPrefix || slug,
        icon: row.icon ?? null,
        color: row.color ?? null,
        fields: Array.isArray(row.fields) ? row.fields : [],
    };
}
/** Friendly display names for cms-core field types (design's type badges). */
export const FIELD_TYPE_LABELS = {
    text: 'Short Text',
    richText: 'Rich Text',
    number: 'Number',
    boolean: 'Boolean',
    date: 'Date',
    slug: 'Slug',
    select: 'Select',
    relationship: 'Relation',
    media: 'Media',
    json: 'JSON',
    color: 'Color',
    url: 'URL',
    email: 'Email',
    array: 'List',
    blocks: 'Blocks',
};
export function fieldTypeLabel(type) {
    return FIELD_TYPE_LABELS[type] ?? type;
}
/** "clientName" / "client_name" → "Client Name" for display. */
export function humanizeFieldName(name) {
    return name
        .replace(/[_-]+/g, ' ')
        .replace(/([a-z0-9])([A-Z])/g, '$1 $2')
        .replace(/\s+/g, ' ')
        .trim()
        .replace(/\b\w/g, (c) => c.toUpperCase());
}
/** "Client Name" → "clientName" for generated field keys. */
export function fieldKeyFromName(name) {
    const words = name
        .replace(/[^a-zA-Z0-9]+/g, ' ')
        .trim()
        .split(/\s+/)
        .filter(Boolean);
    if (words.length === 0)
        return '';
    return words
        .map((w, i) => (i === 0 ? w.toLowerCase() : w[0].toUpperCase() + w.slice(1).toLowerCase()))
        .join('');
}
/**
 * Paste-ready field entry for the collection's `fields` map in
 * `actuate.config.ts` — same escape rules as the New Post Type snippet.
 */
export function renderFieldSnippet(input) {
    const esc = (s) => s.replace(/\\/g, '\\\\').replace(/'/g, "\\'");
    const parts = [`type: '${input.type}'`, `label: '${esc(input.label)}'`];
    if (input.required)
        parts.push('required: true');
    if (input.helpText?.trim())
        parts.push(`admin: { description: '${esc(input.helpText.trim())}' }`);
    if (input.type === 'select') {
        parts.push(`options: [{ label: 'Option A', value: 'option-a' }]`);
    }
    if (input.type === 'relationship') {
        parts.push(`relationTo: 'posts'`);
    }
    return `${input.key}: { ${parts.join(', ')} },`;
}
//# sourceMappingURL=post-types-service.js.map