import http from './client' import type { BatchDeleteResult, PageQuery, PageResult } from './types' /** Mirrors `app.schemas.section_field.SectionFieldRead`. */ export interface SectionField { id: number /** Display name, numbering included — e.g. `1. Introduction`. */ name: string /** Heading depth: 1 for `1.`, 2 for `1.1`. A rendering hint, not a parent. */ level: number /** Points. Fractional, because 五号 = 10.5pt. */ font_size: number /** Canonical RGB as `#RRGGBB`. */ font_color: string created_at: string updated_at: string } /** Body for creating or updating a library field. */ export interface SectionFieldPayload { name: string level: number font_size: number font_color: string } export interface SectionFieldQuery extends PageQuery { level?: number | null } /** The API caps `page_size` at 500; 200 keeps responses modest while looping. */ const LIBRARY_PAGE_SIZE = 200 export async function listSectionFields( query: SectionFieldQuery = {}, ): Promise> { // Drop empty filters so the URL stays clean and the backend sees "no filter" // rather than `keyword=`. const params: Record = {} if (query.keyword) params.keyword = query.keyword if (query.level != null) params.level = query.level if (query.page) params.page = query.page if (query.page_size) params.page_size = query.page_size const { data } = await http.get>('/section-fields', { params }) return data } /** * Fetch the entire library, following pagination. * * The template form needs every field, not one page, so that a field placed * deep in the library can still be shown by name in an existing selection. */ export async function fetchAllSectionFields(keyword?: string): Promise { const all: SectionField[] = [] let page = 1 for (;;) { const result = await listSectionFields({ keyword, page, page_size: LIBRARY_PAGE_SIZE, }) all.push(...result.items) if (page >= result.pages || result.items.length === 0) { return all } page += 1 } } export async function createSectionField( payload: SectionFieldPayload, ): Promise { const { data } = await http.post('/section-fields', payload) return data } export async function updateSectionField( id: number, payload: Partial, ): Promise { const { data } = await http.patch(`/section-fields/${id}`, payload) return data } export async function deleteSectionField(id: number): Promise { await http.delete(`/section-fields/${id}`) } export async function batchDeleteSectionFields( ids: number[], ): Promise { const { data } = await http.post('/section-fields/batch-delete', { ids, }) return data }