export default () => ({ pollingInterval: null, errorCheckInterval: null, authErrorShown: false, startTime: null, pollCount: 0, MAX_POLL_COUNT: 30, // Toggled while window.nostr.signEvent is awaiting the wallet so we can // pause wire:poll, preventing it from racing with auth()->login()'s // session-id migration (which would otherwise yield a 419 on the next // round-trip). nostrLoginInProgress: false, async init() { this.startTime = Date.now(); }, async resolveChallenge() { // 1) Prefer the data-attribute rendered straight from Blade. This avoids // Livewire's $wire proxy entirely and survives any reactive snapshot // quirks that have surfaced behind HTTP caches on production. const fromDataset = this.$root?.dataset?.nostrChallenge; if (typeof fromDataset === 'string' && fromDataset !== '') { return fromDataset; } // 2) Fallback to the Livewire snapshot via $wire. const livewireComponent = this.$el.closest('[wire\\:id]')?.__livewire; const fromWire = livewireComponent?.$wire?.nostrChallenge; if (typeof fromWire === 'string' && fromWire !== '') { return fromWire; } // 3) Last resort: ask the server for a freshly issued challenge. if (livewireComponent?.$wire?.requestNostrChallenge) { try { const refreshed = await livewireComponent.$wire.requestNostrChallenge(); if (typeof refreshed === 'string' && refreshed !== '') { return refreshed; } } catch (error) { console.error('requestNostrChallenge failed:', error); } } return null; }, async openNostrLogin() { // Flip the flag immediately so the wire:poll