/**
* Property Helpers
* Convert between human-friendly and Notion API formats
*/
import * as RichText from './richtext.js'
/**
* Convert simple property values to Notion API format
* Handles auto-detection of property types and conversion
*/
export function convertToNotionProperties(
properties: Record<string, any>,
schema?: Record<string, string>
): Record<string, any> {
const converted: Record<string, any> = {}
for (const [key, value] of Object.entries(properties)) {
if (value === null || value === undefined) {
converted[key] = value
continue
}
// Auto-detect property type and convert
if (typeof value === 'string') {
// Use schema type if available
const schemaType = schema?.[key]
if (schemaType === 'title') {
converted[key] = { title: [RichText.text(value)] }
} else if (schemaType === 'rich_text') {
converted[key] = { rich_text: [RichText.text(value)] }
} else if (schemaType === 'date') {
converted[key] = { date: { start: value } }
} else if (schemaType === 'url') {
converted[key] = { url: value }
} else if (schemaType === 'email') {
converted[key] = { email: value }
} else if (schemaType === 'phone_number') {
converted[key] = { phone_number: value }
} else if (key === 'Name' || key === 'Title' || key.toLowerCase() === 'title') {
// Fallback: guess title from key name
converted[key] = { title: [RichText.text(value)] }
} else {
// Fallback: default to select
converted[key] = { select: { name: value } }
}
} else if (typeof value === 'number') {
converted[key] = { number: value }
} else if (typeof value === 'boolean') {
converted[key] = { checkbox: value }
} else if (Array.isArray(value)) {
// Could be multi_select, relation, people, files
if (value.length > 0 && typeof value[0] === 'string') {
// Assume multi_select
converted[key] = {
multi_select: value.map((v) => ({ name: v }))
}
} else {
converted[key] = value
}
} else if (typeof value === 'object') {
// Already in Notion format or date/complex object
converted[key] = value
} else {
converted[key] = value
}
}
return converted
}