const crypto=require('hypercore-crypto'),safetyCatch=require('safety-catch'),SignalPromise=require('signal-promise'),{
createMemberProofJoinOperation}
=require('../../invitations');
const {
Task}
=require('../task');
module.exports=class JoinRoomKeysTask extends Task{
constructor(_0x37f2f2){
_0x5e8c8a={
_0x5161ad:0x97,_0x291911:0x1e0,_0x266f37:0x1ee},_0x58f9d5={
_0x4135dc:0x1b5}
;
super(_0x37f2f2);
const _0x25475d=this.description.joinRoomKeys;
this._keys=_0x25475d.keys,this._isReadOnly=this._keys.status?..isReadOnly,this._identityInfo=null,this._writerInfo=null;
this._roomInfo=null,this._roomPreferences=null,this._room=null,this._signal=null;
this._watcher=null,this._localWatcher=null;
}
._close(){
_0x3b98a8={
_0x349bb1:0x283,_0x2dcd5d:0x190},_0x1ff9ac={
_0x4a1544:0x25,_0x10ba1a:0xa9}
;
if(this._signal)this._signal.notify(new Error("Fast join closed before resolving digest"));
if(this._localWatcher)this.local.unwatch(this._localWatcher);
if(this._room)this.keet.internalReleaseRoom(this._room);
}
async._save(){
_0x5bb5dd={
_0x16e18f:0x47,_0x259ad3:0x1bb},_0x1cb4fe={
_0x3adc92:0x1c6,_0x27ff18:0x16f,_0x4ca469:0x4c}
;
const _0x34bb11=await this.keet.internalEnterLocalTransaction();
try{
await _0x34bb11.insert("@local/writers",this._writerInfo),await _0x34bb11.flush();
}finally{
await this.keet.internalExitLocalTransaction();
}
}
async._appendJoinRequest(){
_0x30a721={
_0x525ad1:0x1a5,_0x610f8f:0x13f},_0x4449a8={
_0x85efb2:0x180,_0x158c5d:0xc5},_0x33441f=await this.keet.internalVerifyOptimisticCore(this._core);
if(!_0x33441f||this.closing)return;
const _0x31e1d5=this.keet.internalCreateDeviceUpdate(this._identityInfo,this._core.key),_0x58f761=this.keet.internalGetMemberUpdate(this._identityInfo),_0xd9f1d6={
'device':_0x31e1d5,'member':_0x58f761},_0x5c57de=this.keet.internalGetHeads(this._keys.initiator,this._room),_0x3aa47d=createMemberProofJoinOperation(this._keys.mailbox,_0xd9f1d6,{'heads':_0x5c57de});
await this.keet.internalAppendOptimisticOperation(this._core.key,_0x3aa47d);
}
async._openRoom(){
_0x45b4df={
_0x2870db:0x106},_0x2a5cd4={
_0x1a6f73:0x64,_0x82804c:0x1df}
;
this._room=this.keet.internalOpenRoom(this._keys.roomKey,this._keys.encryptionKey,{'fastForwardTo':this._keys.fastForwardTo?{'key':this._keys.fastForwardTo}:null,'mailbox':this._keys.mailbox,'bigBatches':!!this._keys.identitySwap});
await this._room.ready();
}
async._run(){
_0x3245e2={
_0x3c2804:0x2f2,_0x101322:0x1dd},_0x29715b={
_0x373053:0x65,_0x30f914:0xb0,_0x60ace5:0x73,_0xe5792a:0x51f,_0x493b0d:0x50d,_0x52062a:0x4fe,_0x5ba2f8:0x513,_0x477cbd:0x528,_0x419057:0x53b,_0x4cb985:0xb1,_0x369d3f:0x8c,_0x10c82a:0x7e,_0x50ded9:0xa8},_0x59d9f2={
_0x3be4d0:0x19b,_0x2d9426:0x118,_0x3b920f:0x6e},_0x41e848={
_0x494883:0x1a6,_0x37dd64:0x11},_0x432f86={
_0x1e08c6:0x36e,_0x37c8b6:0x86}
;
this._identityInfo=await this.keet.internalGetIdentityInfo();
if(this.closing)return;
const [_0x2f906c,_0x4ec401,_0x204bfd]=await Promise.all([this.local.get("@local/writers",{'mailbox':this._keys.mailbox}),this.local.get("@local/rooms",{'roomKey':this._keys.roomKey}),this.local.get("@local/room-preferences",{'roomKey':this._keys.roomKey})]);
this._writerInfo=_0x2f906c,this._roomInfo=_0x4ec401,this._roomPreferences=_0x204bfd;
let _0x1af1b9=null;
this._writerInfo?this._core=this.store.get(this._writerInfo.key):(_0x1af1b9=crypto.keyPair(),this._core=this.store.get({'keyPair':_0x1af1b9}));
await this._core.ready();
if(this.closing)return;
if(this._roomInfo)await this._openRoom();
if(this.closing)return;
!this._writerInfo&&(this._writerInfo={'keyPair':_0x1af1b9,'key':this._core.key,'mailbox':this._keys.mailbox,'responseKeyPair':null,'roomKey':null});
this._writerInfo.roomKey=this._keys.roomKey;
this._core.length===-0x2fbc&&!this._isReadOnly&&await this._appendJoinRequest();
if(this.closing)return;
await this._save();
if(this.closing)return;
if(!this._room)await this._openRoom();
await this._room.base.setLocal(this._core.key),this._signal=new SignalPromise(),this._localWatcher=async()=>{
try{
const _0x3219e4=await this.local.get("@local/rooms",{'roomKey':this._keys.roomKey});
if(_0x3219e4)this._signal.notify();
}catch(_0xd2c24c){
safetyCatch(_0xd2c24c);
}
},this.local.watch(this._localWatcher),this._localWatcher();
await this._signal.wait();
}
}
;