try {
    importScripts("config/share.js"), importScripts("config/panel.js")
} catch (e) {
    console.error(e)
}
let meetingId = null,
    transcriptId = null,
    meetingName = null,
    username = "";
chrome.storage.session.setAccessLevel({
    accessLevel: "TRUSTED_AND_UNTRUSTED_CONTEXTS"
});
const getTopics = (e, t) => fetch(`${domainUrl}/api/v2/templates/${e}/topics`, t);
chrome.tabs.onRemoved.addListener(((e, t) => {
    chrome.storage.session.get(["sessionList"], (e => {
        if (e.sessionList) {
            let t = e.sessionList;
            console.log(t), chrome.storage.local.get(["token"], (e => {
                if (e.token) {
                    let o = e.token;
                    fetch(`${domainUrl}/api/v2/templates?quick-note=true`, {
                        method: "GET",
                        headers: {
                            Authorization: `Bearer ${o}`,
                            "Content-Type": "application/json"
                        }
                    }).then((e => {
                        200 === e.status ? e.json().then((e => {
                            e.items.length ? getTopics(e.items[0].id, {
                                method: "GET",
                                headers: {
                                    Authorization: `Bearer ${o}`,
                                    "Content-Type": "application/json"
                                }
                            }).then((s => {
                                s.json().then((s => {
                                    const n = s.items;
                                    let r = [];
                                    if (t.forEach((({
                                            image: e,
                                            person: t,
                                            text: o,
                                            startedAt: s,
                                            endedAt: i,
                                            highlight: a
                                        }) => {
                                            let c = [];
                                            if (a) {
                                                const e = bookmarkList.find((e => e.color === a[0]));
                                                if (e) {
                                                    const t = n.find((t => t.color.toLowerCase() === e.code.toLowerCase()));
                                                    t && (c = [t.id])
                                                }
                                            }
                                            o && r.push({
                                                imageUrl: e,
                                                person: "You" === t ? username : t,
                                                startedAt: s,
                                                endedAt: i || s,
                                                highlights: c,
                                                text: o
                                            })
                                        })), r.length) {
                                        let t = JSON.stringify({
                                                meetingId: meetingId,
                                                meetingName: meetingName,
                                                templateId: e.items[0].id,
                                                transcripts: r,
                                                isEnded: !0
                                            }),
                                            s = {
                                                Authorization: `Bearer ${o}`,
                                                "Content-Type": "application/json"
                                            };
                                        transcriptId ? fetch(`${domainUrl}/api/v1/speeches/google-meet/${transcriptId}`, {
                                            method: "PUT",
                                            headers: s,
                                            body: t
                                        }).then((e => {
                                            200 === e.status ? (console.log("save success"), chrome.tabs.create({
                                                url: `${domainUrl}/transcript/${transcriptId}`
                                            })) : 401 === e.status ? console.log("Expired token") : e.json().then((e => {
                                                console.log(e.message)
                                            }))
                                        })).catch((e => {
                                            console.log(e)
                                        })) : fetch(`${domainUrl}/api/v1/speeches/google-meet`, {
                                            method: "POST",
                                            headers: s,
                                            body: t
                                        }).then((e => {
                                            200 === e.status ? (chrome.tabs.create({
                                                url: `${domainUrl}/transcript/${transcriptId}`
                                            }), console.log("success")) : 401 === e.status ? console.log("Expired token") : e.json().then((e => {
                                                console.log(e.message)
                                            }))
                                        })).catch((e => {
                                            console.log(e)
                                        }))
                                    } else console.log("Empty transcript")
                                }))
                            })).catch((e => reject(e))) : console.log("No quick note template")
                        })) : 401 === e.status ? console.log("Expired token") : e.json().then((e => console.log(e.message)))
                    }))
                }
            }))
        }
    }))
})), chrome.runtime.onMessage.addListener((e => {
    console.log(e), "meetingId" === e.type && (console.log(e.meetingId), meetingId = e.meetingId), "transcriptId" === e.type && (console.log(e.transcriptId), transcriptId = e.transcriptId), "meetingName" === e.type && (meetingName = e.meetingName), "username" === e.type && (username = e.data)
})), chrome.runtime.onInstalled.addListener((e => {
    // e.reason === chrome.runtime.OnInstalledReason.INSTALL && chrome.tabs.create({
    //     url: signupUrl
    // }, (function(e) {}))
})), chrome.runtime.onMessageExternal.addListener((function(e, t, o) {
    t.origin === domainUrl && (o("extension received the token"), chrome.storage.local.set({
        token: e.token,
        refreshToken: e.refreshToken
    }, (function() {})), chrome.tabs.query({
        url: googleMeetUrl
    }, (function(t) {
        t[0] && chrome.tabs.sendMessage(t[0].id, {
            token: e.token,
            refreshToken: e.refreshToken
        }, (function(e) {}))
    })))
})), chrome.runtime.setUninstallURL("https://www.laxis.tech/uninstall-survey");