import { Browser , browser as detectBrowser } from './browser' ; let initialized = false ; export let getImpl = function ( item ) : Promise < string > { return new Promise ( ( res , rej ) => { if ( ! initialized ) { throw new Error ( 'Not loaded' ) ; } res ( '' ) ; } ) ; } ; export let setImpl = function ( key , v ) : Promise < void > { return new Promise ( ( res , rej ) => { if ( ! initialized ) { throw new Error ( 'Not loaded' ) ; } res ( null ) ; } ) ; } ; async function init ( ) { if ( initialized ) { return ; } switch ( detectBrowser ( ) ) { case Browser . edge : case Browser . chrome : { getImpl = function ( item ) { return new Promise ( async ( res , rej ) => { chrome . storage . local . get ( item , function ( items ) { res ( items [ item ] ) ; } ) ; } ) ; } ; setImpl = function ( k , v ) { const o = { } ; o [ k ] = v ; return new Promise ( ( res , _ ) => { chrome . storage . local . set ( o , res ) ; } ) ; } ; break ; } case Browser . safari : { getImpl = function ( item ) { return new Promise ( ( res , rej ) => { res ( safari . extension . secureSettings . getItem ( item ) ) ; } ) ; } ; setImpl = function ( k , v ) { return new Promise ( ( res , rej ) => { res ( safari . extension . secureSettings . setItem ( k , v ) ) ; } ) ; } ; break ; } case Browser . firefox : { getImpl = function ( item ) { return browser . storage . local . get ( item ) . then ( ( items ) => items [ item ] ) . then ( ( v ) => v as string ) ; } ; setImpl = function ( k , v ) { const obj = { } ; obj [ k ] = v ; return browser . storage . local . set ( obj ) ; } ; break ; } default : { throw new Error ( 'Unsupported browser' ) ; } } initialized = true ; } export async function get ( k ) { await init ( ) ; const v = getImpl ( k ) ; return v ; } export async function set ( key , v ) { await init ( ) ; return await setImpl ( key , v ) ; }