// RigLibraryBootstrap.swift // Lyrebird // // Copyright (c) 2026 Joshua Parmenter / realizedsound // SPDX-License-Identifier: MIT import Foundation import LyrebirdShoeGaze /// Setup of the rig library for an app shell, split so nothing blocks the launch path. /// /// ``makeStore()`` returns an auto-resolving store instantly (safe to construct in a synchronous /// initializer and hand to the UI). ``start(_:seedFrom:seedName:)`` then does the blocking work — /// resolving the iCloud-or-local location and the initial load — off the main thread, and seeds the /// library from the current working rig **only after** the real (possibly iCloud) contents are /// known, so a first launch can't create a duplicate "Default" rig on every synced device. @MainActor public enum RigLibraryBootstrap { /// Creates an auto-resolving store. Construct this synchronously at launch; call /// ``start(_:seedFrom:seedName:)`` afterwards (e.g. from a SwiftUI `.task` or a `Task`). public static func makeStore() -> RigLibraryStore { RigLibraryStore() } /// Resolves + loads `store` off the main thread, then seeds it from `shoeGaze` if it is still /// empty after loading (first run). Idempotent enough to call once at launch. /// /// - Parameters: /// - store: The store from ``makeStore()``. /// - shoeGaze: The session whose current rig seeds the library when it is first empty. /// - seedName: Name for the seeded first rig. Defaults to `"Default"`. public static func start( _ store: RigLibraryStore, seedFrom shoeGaze: ShoeGaze, seedName: String = "Default" ) async { await store.start() // Seed only after the (possibly iCloud) library has loaded: an empty library here means a // genuine first run, not a not-yet-synced one. guard store.entries.isEmpty else { return } // First run: populate the library with the shipped starter rigs. These are built in // throwaway offline sessions, so they are independent of the current working graph. The // working session is intentionally left as-is (no `currentRigID`) — the user picks a starter // rig from the sidebar to load it, rather than the highlight pointing at a rig the working // graph has not actually loaded. // Seed the shipped starter rigs. `store.save` updates the in-memory library synchronously // (so the session sees every rig immediately) and persists to disk asynchronously — a disk // failure is logged by the store, not thrown here, so there is no per-entry rollback to do. // `save` throws only on a synchronous encode failure of this deterministic rig data, which // should not happen at runtime; if the build or an encode somehow fails, fall back to // seeding the working rig so the library is never left empty on first run. do { let entries: [RigEntry] = try FactoryRigLibrary.makeEntries() for entry: RigEntry in entries { try store.save(entry) } } catch { print("[RigLibraryBootstrap] seeding factory rigs failed: \(error)") seedWorkingRig(store, seedFrom: shoeGaze, seedName: seedName) } } /// Seeds the working session's current rig as a single named library entry. /// /// Last-resort fallback used only when the factory rigs could not be built; keeps the library /// from being left empty on first run. private static func seedWorkingRig( _ store: RigLibraryStore, seedFrom shoeGaze: ShoeGaze, seedName: String ) { guard !shoeGaze.boards.isEmpty else { return } let entry: RigEntry = shoeGaze.captureEntry(name: seedName) do { try store.save(entry) shoeGaze.currentRigID = entry.id } catch { print("[RigLibraryBootstrap] fallback seeding failed: \(error)") } } }