import{a as e,d as t,f as n,n as r,s as i}from"./index-7yDgay5u.js";import{t as a}from"./defineProperty-BbfpZ9Tg.js";import{F as o,I as s,R as c,U as l,q as u}from"./test.id-DxQrYxPk.js";import{r as d}from"./attachment-encryption-DTWbJYgF.js";import{n as f}from"./fileAssetService-gA4Q8V3n.js";var p=e({channel:`host`,key:`app-lock`,schema:t({enabled:i(),recoveryKeyHash:n().optional()}),defaultValue:{enabled:!1}}),m=u(`IAppLockService`),h=class{constructor(e,t){a(this,`hostService`,void 0),a(this,`fileAssetService`,void 0),a(this,`_serviceBrand`,void 0),a(this,`_onDidChange`,new l),a(this,`onDidChange`,this._onDidChange.event),a(this,`_enabled`,void 0),a(this,`_locked`,void 0),a(this,`deviceAuthRequest`,void 0),a(this,`handleVisibilityChange`,()=>{document.hidden&&this.lock()}),this.hostService=e,this.fileAssetService=t,this._enabled=e.getPreference(p).enabled,this._locked=this._enabled,typeof document<`u`&&document.addEventListener(`visibilitychange`,this.handleVisibilityChange)}get enabled(){return this._enabled}get locked(){return this._locked}lock(){!this._enabled||this._locked||(this._locked=!0,this._onDidChange.fire())}async enable(e){let t=await this.resolveRecoveryKeyHash();return t?await this.matchesRecoveryKeyHash(e,t)?(await this.hostService.savePreference(p,{enabled:!0,recoveryKeyHash:t}),this._enabled=!0,this._onDidChange.fire(),{ok:!0}):{ok:!1,error:`invalid-recovery-key`}:{ok:!1,error:`no-recovery-key`}}async disable(){await this.hostService.savePreference(p,{enabled:!1}),this._enabled=!1,this._locked=!1,this._onDidChange.fire()}async unlockWithDeviceAuth(){if(!this._locked)return!0;this.deviceAuthRequest??(this.deviceAuthRequest=this.hostService.requestDeviceAuth({title:r(`appLock.deviceAuth.title`,`Verify your identity`),subtitle:r(`appLock.deviceAuth.subtitle`,`Unlock Islet Journal to continue`),cancelLabel:r(`common.cancel`,`Cancel`)}).finally(()=>{this.deviceAuthRequest=void 0}));let e=await this.deviceAuthRequest;return e&&this.setUnlocked(),e}async unlockWithRecoveryKey(e){let t=this.hostService.getPreference(p).recoveryKeyHash?.trim()||await this.resolveRecoveryKeyHash();return!t||!await this.matchesRecoveryKeyHash(e,t)?!1:(this.setUnlocked(),!0)}setUnlocked(){this._locked&&(this._locked=!1,this._onDidChange.fire())}async matchesRecoveryKeyHash(e,t){let n=e.trim();return n?await d(n)===t:!1}async resolveRecoveryKeyHash(){let e=this.fileAssetService.getSyncConfig(),t=e?.recoveryKeyHash?.trim();if(t)return t;let n=e?.recoveryKey?.trim();if(n)return d(n)}};h=o([s(0,c),s(1,f)],h);export{m as n,p as r,h as t};
//# sourceMappingURL=appLockService-D-ij0gvy.js.map