import * as functions from 'firebase-functions';

// // Start writing Firebase Functions
// // https://firebase.google.com/docs/functions/typescript
//
// export const helloWorld = functions.https.onRequest((request, response) => {
//  response.send("Hello from Firebase!");
// });



export const onMessageCreate = function.database
.ref('/rooms/{roomid}/message/{messageid}')
.onCreate((snapshot,context)=>{
const roomId=context.params.roomId
const messageId=context.params.messageId
const messageData=snapshot.val()
const text=addpizzazz(messageData.text)
return snapshot.ref.update({text: text})

})

function addpizzazz(text : string): string{
return text.replace()
}

export const onMessageUpdate = function.database
.ref('/rooms/{roomid}/message/{messageid}')
.onUpdate((change,context)=>{

const before=change.before.val()
const after=change.after.val()
if (before.text===after.text){
return null
}


const text=addpizzazz(after.text)

const timeedited=Date.now()

return chnage.after.ref.update({text: text})

})

function addpizzazz(text : string): string{
return text.replace()
}

/////////////////////////////////////////////////////////////////////

export const onMessageCreate = function.database
.ref('/rooms/{roomid}/message/{messageid}')
.onCreate(async (snapshot,context)=>{
const roomId=context.params.roomId
const messageId=context.params.messageId
const messageData=snapshot.val()
const text=addpizzazz(messageData.text)
await snapshot.ref.update({text: text})


const countRef=snapshot.ref.parent.parent.child('messageCount')
return countRef.transaction(count=>{
return count+1

})
})


export const onMessageDelete = function.database
.ref('/rooms/{roomid}/message/{messageid}')
.onDelete(async (snapshot,context)=>{


const countRef=snapshot.ref.parent.parent.child('messageCount')
return countRef.transaction(count=>{
return count-1

})
})

