import type { BioTemplateEnvelope, TemplateCollection, TemplateCollectionEnvelope, TemplateId, } from './types' import { isRecord } from './builderReadUtils' /** @deprecated Scheduled for removal from the core SDK in MINT 1.4; the biology templates move to a dedicated package or the owning plugins. */ export const TEMPLATE_COLLECTION_KEY = 'templates' /** @deprecated Scheduled for removal from the core SDK in MINT 1.4; the biology templates move to a dedicated package or the owning plugins. */ export function createTemplateEnvelope( templateId: TemplateId, data: TData, metadata: Record = {}, templateVersion = '1.0' ): BioTemplateEnvelope { return { template_id: templateId, template_version: templateVersion, kind: 'experiment-design', data, metadata, } } /** @deprecated Scheduled for removal from the core SDK in MINT 1.4; the biology templates move to a dedicated package or the owning plugins. */ export function createTemplateCollection( templates: Array>, metadata?: Record ): TemplateCollectionEnvelope { const collection: TemplateCollection = {} for (const template of templates) { assertGenericTemplateEnvelope(template) const templateId = template.template_id if (collection[templateId]) { throw new Error(`Duplicate template_id '${templateId}' in template collection.`) } collection[templateId] = template } return metadata === undefined ? { [TEMPLATE_COLLECTION_KEY]: collection } : { [TEMPLATE_COLLECTION_KEY]: collection, metadata } } /** @deprecated Scheduled for removal from the core SDK in MINT 1.4; the biology templates move to a dedicated package or the owning plugins. */ export function extractTemplateCollection(value: unknown): TemplateCollection { if (isEnvelope(value)) { assertGenericTemplateEnvelope(value) return { [value.template_id]: value } } if (!isRecord(value)) { throw new Error('Template collection payload must be an object.') } const rawCollection = value[TEMPLATE_COLLECTION_KEY] if (rawCollection === undefined) { return {} } if (!isRecord(rawCollection)) { throw new Error('Template collection must be an object.') } const collection: TemplateCollection = {} for (const [templateId, template] of Object.entries(rawCollection)) { assertGenericTemplateEnvelope(template) if (template.template_id !== templateId) { throw new Error( `Template collection key '${templateId}' does not match envelope template_id '${template.template_id}'.` ) } collection[templateId] = template } return collection } /** @deprecated Scheduled for removal from the core SDK in MINT 1.4; the biology templates move to a dedicated package or the owning plugins. */ export function ensureTemplateFromCollection>( value: unknown, templateId: TemplateId | string ): TTemplate { const collection = extractTemplateCollection(value) const template = collection[templateId] if (!template) { throw new Error(`Template '${templateId}' was not found in template collection.`) } assertTemplateEnvelope(template, templateId) return template as TTemplate } /** @deprecated Scheduled for removal from the core SDK in MINT 1.4; the biology templates move to a dedicated package or the owning plugins. */ export function assertTemplateEnvelope( value: unknown, templateId: TemplateId | string ): asserts value is BioTemplateEnvelope { assertGenericTemplateEnvelope(value) if (value.template_id !== templateId) { throw new Error(`Expected template_id '${templateId}', got '${String(value.template_id)}'.`) } } /** @deprecated Scheduled for removal from the core SDK in MINT 1.4; the biology templates move to a dedicated package or the owning plugins. */ export function ensureTemplateEnvelope>( value: unknown, templateId: TemplateId | string ): TTemplate { assertTemplateEnvelope(value, templateId) return value as TTemplate } /** @deprecated Scheduled for removal from the core SDK in MINT 1.4; the biology templates move to a dedicated package or the owning plugins. */ export function getTemplateData( template: BioTemplateEnvelope | TData, templateId?: TemplateId | string ): TData { if (isEnvelope(template)) { if (templateId) assertTemplateEnvelope(template, templateId) return template.data } return template } function assertGenericTemplateEnvelope(value: unknown): asserts value is BioTemplateEnvelope { if (!isRecord(value)) { throw new Error('Template envelope must be an object.') } if (typeof value.template_id !== 'string' || value.template_id.length === 0) { throw new Error('Template envelope requires a template_id string.') } if (typeof value.template_version !== 'string' || value.template_version.length === 0) { throw new Error('Template envelope requires a template_version string.') } if (!isRecord(value.data)) { throw new Error('Template envelope data must be an object.') } if ('metadata' in value && !isRecord(value.metadata)) { throw new Error('Template envelope metadata must be an object.') } } function isEnvelope(value: unknown): value is BioTemplateEnvelope { return isRecord(value) && 'template_id' in value && 'data' in value }