// @generated by scripts/build-runtime.mjs; do not edit. // @ts-nocheck -- generated JavaScript uses a .ts extension for Pi's Jiti loader. import { setSyncSetupCompletions } from "./chunk-4U6AFLJB.ts"; import { SetupPullRequiresUiError, safeTerminalText } from "./chunk-W64NEHXT.ts"; import { configuredSyncSetupNames, loadConfig, normalizeOnSwitch, syncConfigReviewIdentity, syncSetupReviewIdentity, updateLocalConfig } from "./chunk-APZVBSM6.ts"; // src/setup-switch.ts var SETUP_SWITCH_ACTION_OPTIONS = [ { label: "Ask before pull", value: "ask-before-pull" }, { label: "Start pull", value: "pull-after-switch" }, { label: "Switch only", value: "switch-only" } ]; function setupSwitchActionLabel(action) { return SETUP_SWITCH_ACTION_OPTIONS.find((option) => option.value === action)?.label ?? action; } function setupSwitchActionFromLabel(label) { return SETUP_SWITCH_ACTION_OPTIONS.find((option) => option.label === label)?.value; } async function saveOnSwitch(action, signal) { await updateLocalConfig((settings) => ({ ...settings, onSwitch: action }), signal); } async function useSyncSetup(ctx, name, pullCurrentSetup, expectedAction, signal, expectedSetupIdentity) { const normalized = name.trim(); if (!normalized) throw new Error("Usage: /sync use "); const loadedConfig = await loadConfig(normalized); const reviewedSetupIdentity = expectedSetupIdentity ?? syncConfigReviewIdentity(loadedConfig); throwIfAborted(signal); const switchResult = { action: "ask-before-pull", switched: false }; await updateLocalConfig((current) => { throwIfAborted(signal); const setup = current.syncSetups[normalized]; if (!setup) { throw new Error(`Sync setup \u201C${safeTerminalText(normalized)}\u201D no longer exists.`); } const connectionName = setup.storage.connection; const connection = current.storageConnections[connectionName]; if (!connection || syncSetupReviewIdentity(normalized, setup, connectionName, connection) !== reviewedSetupIdentity) { throw new Error( `Sync setup \u201C${safeTerminalText(normalized)}\u201D changed while the switch preview was open; reopen it and review the current destination.` ); } switchResult.action = normalizeOnSwitch(current.onSwitch); if (expectedAction !== void 0 && switchResult.action !== expectedAction) { throw new Error( "Setup-switch behavior changed while the preview was open; reopen it and retry." ); } if (switchResult.action === "pull-after-switch" && !ctx.hasUI) { throw new SetupPullRequiresUiError( `Automatic setup pulls require interactive confirmation; sync setup \u201C${safeTerminalText(normalized)}\u201D was not switched. Use TUI or RPC mode.` ); } if (current.activeSyncSetup === normalized) return current; switchResult.switched = true; return { ...current, activeSyncSetup: normalized }; }, signal); throwIfAborted(signal); if (!switchResult.switched) { ctx.ui.notify(`Sync setup \u201C${safeTerminalText(normalized)}\u201D is already current.`, "info"); return { pullApplied: false }; } setSyncSetupCompletions(await configuredSyncSetupNames()); throwIfAborted(signal); if (switchResult.action === "switch-only") { ctx.ui.notify(`Switched to \u201C${safeTerminalText(normalized)}\u201D. No files were pulled.`, "info"); return { pullApplied: false }; } if (switchResult.action === "ask-before-pull") { if (ctx.mode !== "tui") { ctx.ui.notify( `Switched to \u201C${safeTerminalText(normalized)}\u201D. No files were pulled because confirmation requires TUI mode; run /sync pull to apply this setup.`, "info" ); return { pullApplied: false }; } const confirmed = await ctx.ui.confirm( `Review a pull for sync setup \u201C${safeTerminalText(normalized)}\u201D now?`, "pi-sync will check the remote snapshot and show the exact local writes and deletions before applying anything.", { signal } ); throwIfAborted(signal); if (!confirmed) { ctx.ui.notify( `Switched to \u201C${safeTerminalText(normalized)}\u201D; files were not pulled.`, "info" ); return { pullApplied: false }; } } ctx.ui.notify( `Switched to \u201C${safeTerminalText(normalized)}\u201D. Checking remote files for a reviewed pull\u2026`, "info" ); if (!pullCurrentSetup) { throw new Error(`Switched to \u201C${safeTerminalText(normalized)}\u201D, but pull is unavailable.`); } const pullOutcome = await pullCurrentSetup(normalized); throwIfAborted(signal); if (pullOutcome === "cancelled") { ctx.ui.notify( `Pull cancelled; sync setup \u201C${safeTerminalText(normalized)}\u201D remains current and synced files were not changed.`, "info" ); } return { pullApplied: pullOutcome === "applied" }; } function throwIfAborted(signal) { if (!signal?.aborted) return; throw signal.reason instanceof Error ? signal.reason : new DOMException("The operation was aborted", "AbortError"); } export { SETUP_SWITCH_ACTION_OPTIONS, setupSwitchActionLabel, setupSwitchActionFromLabel, saveOnSwitch, useSyncSetup }; //# sourceMappingURL=chunk-BHJUISB4.ts.map