{
  "$schema": "https://ui.shadcn.com/schema/registry-item.json",
  "name": "core-date-picker",
  "type": "registry:lib",
  "title": "core/date-picker",
  "description": "Framework-free behaviour shared by the ui-* components (date-picker); no DOM, no lit. Copied from @purpurabog/ui 2.3.0; the package is the maintained source, this copy is yours to change. Icons from the Carbon Design System (Apache-2.0, © IBM Corp.; see THIRD-PARTY-NOTICES.md in the package).",
  "dependencies": [],
  "registryDependencies": [
    "@kanok-ui/core-date"
  ],
  "files": [
    {
      "path": "src/core/date-picker.ts",
      "type": "registry:lib",
      "target": "components/ui/core/date-picker.ts",
      "content": "/**\n * The date picker's state and every change to it, framework-free: a pure reducer (Carbon's\n * DatePicker / flatpickr rules, as `<ui-date-picker>` and the React DatePicker behave). Each action\n * returns a new state and leaves the old one alone; moving focus and showing the popover stay with\n * the renderer. A value the user changes (typing a full date, committing, picking, clearing) is\n * what the renderer reports as a change, once committed (a typed full date on its blur or Enter);\n * `sync` (a new value from the app) and `reset` (a form reset) change values without that.\n */\nimport { clampDate, formatDate, fromIso, isUnavailable, parseDate, pickInRange, toIso, type DateFormat, type DateLimits, type Era, type RangeField } from './date.ts';\n\nexport type DateField = RangeField;\n\nexport interface DatePickerState {\n    /** What each box shows (possibly half-typed). */\n    readonly text: Readonly<Record<DateField, string>>;\n    /** The committed ISO dates ('' for none). */\n    readonly value: Readonly<Record<DateField, string>>;\n    readonly open: boolean;\n    /** The calendar day that has (or will get) focus. */\n    readonly focusDate: Date;\n    /** A commit or submit happened: problems show from now on. */\n    readonly touched: boolean;\n    /** The box the calendar picks for. */\n    readonly activeField: DateField;\n    /** The box that last had focus: Tab / Shift+Tab / Esc in the calendar act relative to it. */\n    readonly lastField: DateField;\n}\n\n/** The picker's settings an action needs (from its props). */\nexport interface DatePickerContext {\n    format: DateFormat;\n    era: Era;\n    limits: DateLimits;\n    range: boolean;\n}\n\nexport type DatePickerAction =\n    /** The box's text changed; text that already reads as an available date in the format is committed at once (Carbon). */\n    | { type: 'input'; field: DateField; text: string }\n    /** Blur / Enter: a readable date is formatted and kept (even refused, so the box can say why), anything else empties the value. */\n    | { type: 'commit'; field: DateField }\n    /** As `commit`, unless the text already spells the committed value. */\n    | { type: 'commitIfEdited'; field: DateField }\n    /** Open the calendar for `field`, on its typed or committed date, else the start, else `today`. */\n    | { type: 'open'; field: DateField; today: Date }\n    /** Close and commit what was typed in each box. */\n    | { type: 'close' }\n    /** A day picked in the calendar (refused days are ignored); a range keeps it open for the end. */\n    | { type: 'select'; date: Date }\n    /** A box got focus (it becomes the active and last box). */\n    | { type: 'focus'; field: DateField }\n    /** The calendar picks for `field` from now on. */\n    | { type: 'activate'; field: DateField }\n    /** A box lost focus to something outside the calendar: commit it; `leaving` the picker closes it. */\n    | { type: 'blur'; field: DateField; leaving: boolean }\n    /** The keyboard moved the calendar's focus. */\n    | { type: 'focusDate'; date: Date }\n    /** A form submit found the box invalid. */\n    | { type: 'touch' }\n    /** A date from the operating system's picker. */\n    | { type: 'nativePick'; field: DateField; iso: string }\n    /** The × button. */\n    | { type: 'clear'; field: DateField }\n    /** A new value from the app: its box shows it, and an open calendar for that box moves to it. */\n    | { type: 'sync'; field: DateField; iso: string }\n    /** A form reset: back to these dates, closed and untouched. */\n    | { type: 'reset'; start: string; end: string };\n\nconst fieldsOf = (ctx: DatePickerContext): DateField[] => (ctx.range ? ['start', 'end'] : ['start']);\nconst fmt = (d: Date, ctx: DatePickerContext) => formatDate(d, ctx.format, ctx.era);\nconst textOf = (iso: string, ctx: DatePickerContext) => {\n    const d = fromIso(iso);\n    return d ? fmt(d, ctx) : '';\n};\nconst validIso = (iso: string) => (fromIso(iso) ? iso : '');\n\n/** The first state: these dates in their boxes (something that isn't a date shows nothing), closed. */\nexport function initialDatePickerState(start: string, end: string, ctx: DatePickerContext, today: Date): DatePickerState {\n    return {\n        text: { start: textOf(start, ctx), end: textOf(end, ctx) },\n        value: { start: validIso(start), end: validIso(end) },\n        open: false,\n        focusDate: today,\n        touched: false,\n        activeField: 'start',\n        lastField: 'start',\n    };\n}\n\n/** The typed text, when it already reads as a complete date in the box's own format. */\nexport function typedFullDate(state: DatePickerState, field: DateField, ctx: DatePickerContext): Date | null {\n    const result = parseDate(state.text[field], ctx.format, ctx.era);\n    return 'date' in result && fmt(result.date, ctx) === state.text[field] ? result.date : null;\n}\n\nconst withField = <T>(record: Readonly<Record<DateField, T>>, field: DateField, next: T) => (record[field] === next ? record : { ...record, [field]: next });\n\nfunction setBox(state: DatePickerState, field: DateField, text: string, value: string): DatePickerState {\n    const nextText = withField(state.text, field, text);\n    const nextValue = withField(state.value, field, value);\n    return nextText === state.text && nextValue === state.value ? state : { ...state, text: nextText, value: nextValue };\n}\n\nfunction commit(state: DatePickerState, field: DateField, ctx: DatePickerContext): DatePickerState {\n    const touched = state.touched ? state : { ...state, touched: true };\n    const result = parseDate(state.text[field], ctx.format, ctx.era);\n    if ('date' in result) return setBox(touched, field, fmt(result.date, ctx), toIso(result.date));\n    return setBox(touched, field, touched.text[field], '');\n}\n\nfunction commitIfEdited(state: DatePickerState, field: DateField, ctx: DatePickerContext): DatePickerState {\n    return state.text[field] === textOf(state.value[field], ctx) ? state : commit(state, field, ctx);\n}\n\nfunction close(state: DatePickerState, ctx: DatePickerContext): DatePickerState {\n    const closed = state.open ? { ...state, open: false } : state;\n    return fieldsOf(ctx).reduce((s, f) => commitIfEdited(s, f, ctx), closed);\n}\n\nfunction select(state: DatePickerState, date: Date, ctx: DatePickerContext): DatePickerState {\n    if (isUnavailable(date, ctx.limits)) return state;\n    const touched = state.touched ? state : { ...state, touched: true };\n    if (!ctx.range) return close(setBox(touched, 'start', fmt(date, ctx), toIso(date)), ctx);\n    const iso = toIso(date);\n    const next = pickInRange({ start: touched.value.start, end: touched.value.end, active: touched.activeField }, iso);\n    let picked: DatePickerState = touched;\n    for (const f of fieldsOf(ctx)) {\n        const v = f === 'start' ? next.start : next.end;\n        if (v === picked.value[f] && v !== iso) continue;\n        picked = setBox(picked, f, textOf(v, ctx), v);\n    }\n    if (next.done) return close(picked, ctx);\n    return picked.activeField === next.active ? picked : { ...picked, activeField: next.active }; // stays open for the end\n}\n\nfunction input(state: DatePickerState, field: DateField, text: string, ctx: DatePickerContext): DatePickerState {\n    const typed = setBox(state, field, text, state.value[field]);\n    const full = typedFullDate(typed, field, ctx);\n    if (!full || isUnavailable(full, ctx.limits)) return typed;\n    const committed = setBox(typed, field, text, toIso(full));\n    return committed.open ? { ...committed, focusDate: clampDate(full, ctx.limits) } : committed; // the grid follows; focus stays in the box\n}\n\nfunction open(state: DatePickerState, field: DateField, today: Date, ctx: DatePickerContext): DatePickerState {\n    const start = typedFullDate(state, field, ctx) ?? fromIso(state.value[field]) ?? fromIso(state.value.start) ?? today;\n    return { ...state, open: true, activeField: field, focusDate: clampDate(start, ctx.limits) };\n}\n\nfunction sync(state: DatePickerState, field: DateField, iso: string, ctx: DatePickerContext): DatePickerState {\n    const d = fromIso(iso);\n    const synced = setBox(state, field, d ? fmt(d, ctx) : '', d ? iso : '');\n    return d && synced.open && synced.activeField === field ? { ...synced, focusDate: clampDate(d, ctx.limits) } : synced;\n}\n\n/** The next state for `action` (the same object when nothing changes). */\nexport function datePickerReducer(state: DatePickerState, action: DatePickerAction, ctx: DatePickerContext): DatePickerState {\n    switch (action.type) {\n        case 'input':\n            return input(state, action.field, action.text, ctx);\n        case 'commit':\n            return commit(state, action.field, ctx);\n        case 'commitIfEdited':\n            return commitIfEdited(state, action.field, ctx);\n        case 'open':\n            return open(state, action.field, action.today, ctx);\n        case 'close':\n            return close(state, ctx);\n        case 'select':\n            return select(state, action.date, ctx);\n        case 'focus':\n            return state.activeField === action.field && state.lastField === action.field ? state : { ...state, activeField: action.field, lastField: action.field };\n        case 'activate':\n            return state.activeField === action.field ? state : { ...state, activeField: action.field };\n        case 'blur': {\n            const committed = commitIfEdited(state, action.field, ctx);\n            return action.leaving && committed.open ? { ...committed, open: false } : committed;\n        }\n        case 'focusDate':\n            return { ...state, focusDate: action.date };\n        case 'touch':\n            return state.touched ? state : { ...state, touched: true };\n        case 'nativePick': {\n            const date = fromIso(action.iso);\n            if (!date) return state;\n            const picked = setBox(state, action.field, fmt(date, ctx), toIso(date));\n            return picked.touched ? picked : { ...picked, touched: true };\n        }\n        case 'clear':\n            return setBox(state, action.field, '', '');\n        case 'sync':\n            return sync(state, action.field, action.iso, ctx);\n        case 'reset': {\n            const first = initialDatePickerState(action.start, action.end, ctx, state.focusDate);\n            return { ...state, text: first.text, value: first.value, open: false, touched: false };\n        }\n    }\n}\n\n/** The boxes whose committed value differs between two states (what a renderer reports as changes). */\nexport const changedFields = (before: DatePickerState, after: DatePickerState): DateField[] => (['start', 'end'] as const).filter((f) => before.value[f] !== after.value[f]);\n"
    }
  ]
}
