import {z} from 'zod' export type GetApiEstimatesLineItemNotesSuggestionsArgs = {serviceCatalogItemId?: string | undefined, inventoryItemId?: string | undefined, retailItemId?: string | undefined, billableItemId?: string | undefined, q?: string | undefined, limit?: (number | unknown) | undefined}; export type GetApiEstimatesLineItemNotesSuggestionsOptions = {baseUrl?: string | undefined, Bearer?: string | undefined, fetch?: typeof fetch | undefined}; export const lineItemNoteSuggestionDto = z.object({source: z.string().optional(), text: z.string().optional(), templateId: z.string().nullable().optional()}); export const getApiEstimatesLineItemNotesSuggestionsResponse = z.array(lineItemNoteSuggestionDto); export const validateResponse = async ( schema: { readonly '~standard': { readonly validate: (value: unknown) => ({ readonly value: Output; readonly issues?: undefined } | { readonly issues: ReadonlyArray<{ readonly message: string; readonly path?: ReadonlyArray | undefined }> }) | Promise<{ readonly value: Output; readonly issues?: undefined } | { readonly issues: ReadonlyArray<{ readonly message: string; readonly path?: ReadonlyArray | undefined }> }> } }, value: unknown ): Promise => { const result = await schema['~standard'].validate(value) if (result.issues !== undefined) { const detail = result.issues .map(({ message, path }) => path?.length ? `${path.map((segment) => String(typeof segment === 'object' ? segment.key : segment)).join('.')}: ${message}` : message ) .join('; ') throw new Error(`Response does not match the schema: ${detail}`, { cause: { issues: result.issues } }) } return result.value }; /** * Autocomplete suggestions for the line-item note field (ITE-573 + ITE-574): saved note * templates merged with distinct notes previously used on estimate lines for the same * catalog item. Saved entries carry `source: "saved"` + their `templateId`; * history entries carry `source: "history"` + null. Serves the FE combobox (ITE-575). * * Saved-template candidacy: templates with an attachment to the line's item id, tag * attachments whose Smart Tag is assigned to the line's item (service/inventory lines * only — retail and billable items are not taggable), plus any-item templates * (AppliesToAnyItem, offered on any keyed line); Clinic scope for everyone, Personal * scope only for the owner. A history entry whose trimmed * case-insensitive text equals a saved template's collapses INTO the saved entry * (saved wins). Orphaned templates (item-specific, all items deleted) are never offered. * * The caller sends exactly one item-id param — whichever FK the line being authored keys * on. `PrescriptionItemId` is deliberately NOT a keying param: Rx-created lines always * carry `InventoryItemId` too, and a prescription item is a per-prescription governance * record, not a reusable catalog identity. * * Ranking: q match quality (prefix beats substring) → recency → frequency. De-duped * case-insensitively on trimmed text; blank or <3-char notes excluded. Always 200 with * `[]` on no key / no match. * * GET /Estimates/line-item-notes/suggestions */ export const getApiEstimatesLineItemNotesSuggestions = async (args: GetApiEstimatesLineItemNotesSuggestionsArgs, options?: GetApiEstimatesLineItemNotesSuggestionsOptions) => { const {baseUrl = "https://api.clinicos.vet/backend/business", fetch: fetchFn = fetch, Bearer} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (args.serviceCatalogItemId != null) query.append("serviceCatalogItemId", String(args.serviceCatalogItemId)) if (args.inventoryItemId != null) query.append("inventoryItemId", String(args.inventoryItemId)) if (args.retailItemId != null) query.append("retailItemId", String(args.retailItemId)) if (args.billableItemId != null) query.append("billableItemId", String(args.billableItemId)) if (args.q != null) query.append("q", String(args.q)) if (args.limit != null) query.append("limit", String(args.limit)) if (Bearer !== undefined) headers.set('authorization', `Bearer ${Bearer}`) const url = `${baseUrl.replace(/\/+$/, '')}/Estimates/line-item-notes/suggestions${query.size > 0 ? `?${query}` : ''}` const res = await fetchFn(url, {method: "GET", headers}) if (!res.ok) throw new Error(`Request failed with status ${res.status}`, {cause: {status: res.status, body: await res.text()}}) return validateResponse(getApiEstimatesLineItemNotesSuggestionsResponse, await res.json()) };