var lockerList = [];
var fetchPromise;
var mainPromise;
function _fetchLockerListLink() {
    if (fetchPromise) return fetchPromise;
    fetchPromise = fetch(getLatestLockerListLinkEndpoint).then(resp => {
        return resp.json();
    });
    return fetchPromise;
}
function fetchLockerListLink() {
    if (mainPromise) return mainPromise;
    mainPromise = new Promise(async (resolve, reject) => {
        // if (lockerList.length) return resolve(lockerList);
        const data = await _fetchLockerListLink()
        if (data && data.success && data.data?.couriers?.length) {
            for (const courier of data.data.couriers) {
                const cacheKey = `shipany_lockers_${courier.cour_uid}`;
                const filterLocker = (locker) => {
                    return courier?.detail_location_types?.[locker.detail_loc_typ]?.enabled &&
                        locker.supported_storage_types?.some(supported_storage_type => data.data.couriers_info.includes(`${locker.courier_id}|${supported_storage_type}|${locker.addr.cnty}`));
                }

                try {
                    const cachedData = await localforage.getItem(cacheKey);

                    if (cachedData && (cachedData.timestamp > Date.now() - 3600 * 1000)) { // 1 hour
                        lockerList = lockerList.concat(cachedData.data.filter(filterLocker));
                        continue;
                    } else if (cachedData) {
                        await localforage.removeItem(cacheKey);
                    }

                    for (const url of courier.urls) {
                        const lockerResp = await fetch(url);
                        if (lockerResp.ok) {
                            try {
                                let lockerData;
                                try {
                                    lockerData = await lockerResp.clone().json();
                                } catch (e) {
                                    const resp = await lockerResp.clone().text();
                                    if (resp === "<html>\n<body>\n</body>\n</html>\n") {
                                        continue;
                                    }
                                    console.error('error', e);
                                }
                                lockerList = lockerList.concat(lockerData.filter(filterLocker));
                                await localforage.setItem(cacheKey, {
                                    timestamp: Date.now(),
                                    data: lockerData
                                });
                            } catch (e) {
                                console.error('error', e);
                            }
                        }
                    }
                } catch (err) {
                    console.error('localforage error:', err);
                }
            }
        }
        resolve(lockerList);
    });
    return mainPromise;
}

function translateRegion(val) {
    if (val === 'Hongkong Island') val = '香港';
    else if (val === 'Kowloon') val = '九龍';
    else if (val === 'New Territories') val = '新界';
    else if (val === 'Outlying Islands') val = '離島';
    else if (val === 'Macao') val = '澳門';
    else if (val === 'All Regions') val = '全部區域';
    return val;
}

function translateDistrict(val) {
    if (val === 'Central and Western District') val = '中西區';
    else if (val === 'Cheung Chau District') val = '長洲區';
    else if (val === 'Eastern District') val = '東區';
    else if (val === 'Kowloon City District') val = '九龍城區';
    else if (val === 'Kwai Tsing District') val = '葵青區';
    else if (val === 'Kwun Tong District') val = '觀塘區';
    else if (val === 'Lamma Island District') val = '南丫島區';
    else if (val === 'Lantau District') val = '大嶼山區';
    else if (val === 'North District') val = '北區';
    else if (val === 'Peng Chau District') val = '坪洲區';
    else if (val === 'Sai Kung District') val = '西貢區';
    else if (val === 'Sha Tin District') val = '沙田區';
    else if (val === 'Sham Shui Po District') val = '深水埗區';
    else if (val === 'Southern District') val = '南區';
    else if (val === 'Tai Po District') val = '大埔區';
    else if (val === 'Tsuen Wan District') val = '荃灣區';
    else if (val === 'Tuen Mun District') val = '屯門區';
    else if (val === 'Wan Chai District') val = '灣仔區';
    else if (val === 'Wong Tai Sin District') val = '黃大仙區';
    else if (val === 'Yau Tsim Mong District') val = '油尖旺區';
    else if (val === 'Yuen Long District') val = '元朗區';
    else if (val === 'Islands District') val = '離島區';
    else if (val === 'Peninsula de Macao') val = '澳門半島';
    else if (val === 'Ilha da Taipa') val = '氹仔';
    else if (val === 'Taipa Island"') val = '路氹城';
    else if (val === 'Coloane') val = '路環';
    else if (val === 'Macao') val = '澳門';
    else if (val === 'All Districts') val = '全部地區';
    return val;
}

function translateArea(val) {
    if (val === 'Admiralty') val = '金鐘'
    else if (val === 'Kennedy Town') val = '堅尼地城'
    else if (val === 'Central') val = '中環'
    else if (val === 'Lugard Road') val = '山頂盧吉道'
    else if (val === 'Mid-levels') val = '半山'
    else if (val === 'Sai Wan') val = '西環'
    else if (val === 'Sai Ying Pun') val = '西營盤'
    else if (val === 'Sheung Wan') val = '上環'
    else if (val === 'The Peak') val = '山頂'
    else if (val === 'Cheung Chau') val = '長洲'
    else if (val === 'Chai Wan') val = '柴灣'
    else if (val === 'Fortress Hill') val = '炮台山'
    else if (val === 'Heng Fa Chuen') val = '杏花邨'
    else if (val === 'North Point') val = '北角'
    else if (val === 'Quarry Bay') val = '鰂魚涌'
    else if (val === 'Sai Wan Ho') val = '西灣河'
    else if (val === 'Shau Kei Wan') val = '筲箕灣'
    else if (val === 'Siu Sai Wan') val = '小西灣'
    else if (val === 'Tai Koo') val = '太古'
    else if (val === 'Taikoo Shing') val = '太古城'
    else if (val === 'Tin Hau') val = '天后'
    else if (val === 'Ho Man Tin') val = '何文田'
    else if (val === 'Hung Hom') val = '紅磡'
    else if (val === 'Kai Tak') val = '啟德'
    else if (val === 'Kowloon City') val = '九龍城'
    else if (val === 'Kowloon Tong') val = '九龍塘'
    else if (val === 'Ma Tau Wai') val = '馬頭圍'
    else if (val === 'To Kwa Wan') val = '土瓜灣'
    else if (val === 'Whampoa') val = '黃埔'
    else if (val === 'Kwai Chung') val = '葵涌'
    else if (val === 'Kwai Fong') val = '葵芳'
    else if (val === 'Kwai Hing') val = '葵興'
    else if (val === 'Kwai Tsing Container Terminals') val = '葵青貨櫃碼頭'
    else if (val === 'Lai King') val = '荔景'
    else if (val === 'Tsing Yi') val = '青衣'
    else if (val === 'Tsing Yi Goodman Interlink') val = '青衣嘉民領達中心'
    else if (val === 'Kowloon Bay') val = '九龍灣'
    else if (val === 'Kwun Tong') val = '觀塘'
    else if (val === 'Lam Tin') val = '藍田'
    else if (val === 'Ngau Tau Kok') val = '牛頭角'
    else if (val === 'Sau Mau Ping') val = '秀茂坪'
    else if (val === 'Shun Lee') val = '順利'
    else if (val === 'Yau Tong') val = '油塘'
    else if (val === 'Lamma Island') val = '南丫島'
    else if (val === 'Chek Lap Kok') val = '赤鱲角'
    else if (val === 'Cheung Sha') val = '長沙'
    else if (val === 'Discovery Bay') val = '愉景灣'
    else if (val === 'Hong Kong Disneyland') val = '香港迪士尼'
    else if (val === 'Hong Kong International Airport') val = '香港國際機場'
    else if (val === 'Lantau Island') val = '大嶼山'
    else if (val === 'Ma Wan') val = '馬灣'
    else if (val === 'Mui Wo') val = '梅窩'
    else if (val === 'Mui Wo Pier') val = '梅窩碼頭'
    else if (val === 'Ngong Ping') val = '昂坪'
    else if (val === 'Penny\'s Bay') val = '竹篙灣'
    else if (val === 'Pui O') val = '貝澳'
    else if (val === 'Shibi') val = '石壁'
    else if (val === 'Tai O') val = '大澳'
    else if (val === 'Tai O Pier') val = '大澳碼頭'
    else if (val === 'The Big Buddha') val = '天壇大佛'
    else if (val === 'Tong Fuk') val = '塘福'
    else if (val === 'Tung Chung') val = '東涌'
    else if (val === 'Fanling') val = '粉嶺'
    else if (val === 'Kwu Tung') val = '古洞'
    else if (val === 'Man Kam To') val = '文錦渡'
    else if (val === 'North District') val = '北區'
    else if (val === 'Ping Che') val = '坪輋'
    else if (val === 'Sha Tau Kok') val = '沙頭角'
    else if (val === 'Sheung Shui') val = '上水'
    else if (val === 'Ta Kwu Ling') val = '打鼓嶺'
    else if (val === 'Peng Chau') val = '坪洲'
    else if (val === 'Clearwater Bay') val = '清水灣'
    else if (val === 'Hang Hau') val = '坑口'
    else if (val === 'LOHAS Park') val = '康城'
    else if (val === 'Po Lam') val = '寶琳'
    else if (val === 'Sai Kung') val = '西貢'
    else if (val === 'Tiu Keng Leng') val = '調景嶺'
    else if (val === 'Tseung Kwan O') val = '將軍澳'
    else if (val === 'Fo Tan') val = '火炭'
    else if (val === 'Kau To Shan/Cove Hill') val = '九肚山'
    else if (val === 'Ma Liu Shui') val = '馬料水'
    else if (val === 'Ma On Shan') val = '馬鞍山'
    else if (val === 'Sha Tin') val = '沙田'
    else if (val === 'Shek Mun') val = '石門'
    else if (val === 'Siu Lek Yuen') val = '小瀝源'
    else if (val === 'Tai Wai') val = '大圍'
    else if (val === 'Wo Che') val = '禾輋'
    else if (val === 'Cheung Sha Wan') val = '長沙灣'
    else if (val === 'Lai Chi Kok') val = '荔枝角'
    else if (val === 'Mei Foo') val = '美孚'
    else if (val === 'Nam Cheong') val = '南昌'
    else if (val === 'Ngong Shuen Chau') val = '昂船洲'
    else if (val === 'Sham Shui Po') val = '深水埗'
    else if (val === 'Shek Kip Mei') val = '石硤尾'
    else if (val === 'Aberdeen') val = '香港仔'
    else if (val === 'Ap Lei Chau') val = '鴨脷洲'
    else if (val === 'Chung Hom Kok') val = '舂坎角'
    else if (val === 'Deep Water Bay') val = '深水灣'
    else if (val === 'Pok Fu Lam') val = '薄扶林'
    else if (val === 'Repulse Bay') val = '淺水灣'
    else if (val === 'Shek O') val = '石澳'
    else if (val === 'Stanley') val = '赤柱'
    else if (val === 'Tai Tam') val = '大潭'
    else if (val === 'Wah Fu') val = '華富'
    else if (val === 'Wong Chuk Hang') val = '黃竹坑'
    else if (val === 'Tai Po') val = '大埔'
    else if (val === 'Tai Wo') val = '太和'
    else if (val === 'Sham Tseng') val = '深井'
    else if (val === 'Tai Wo Hau') val = '大窩口'
    else if (val === 'Ting Kau') val = '汀九'
    else if (val === 'Tsuen Wan') val = '荃灣'
    else if (val === 'Lam Tei') val = '藍地'
    else if (val === 'Lung Kwu Tan') val = '龍鼓灘'
    else if (val === 'San Hui') val = '新墟'
    else if (val === 'Siu Lam') val = '小欖'
    else if (val === 'So Kwun Wat') val = '掃管笏'
    else if (val === 'Tai Lam') val = '大欖'
    else if (val === 'Tai Lam Chung') val = '大欖涌'
    else if (val === 'Tuen Mun') val = '屯門'
    else if (val === 'Tuen Mun River Trade Terminal') val = '屯門內河碼頭'
    else if (val === 'Causeway Bay') val = '銅鑼灣'
    else if (val === 'Happy Valley') val = '跑馬地'
    else if (val === 'Hong Kong Convention and Exhibition Centre') val = '香港會議展覽中心'
    else if (val === 'Tai Hang') val = '大坑'
    else if (val === 'Wan Chai') val = '灣仔'
    else if (val === 'Choi Hung') val = '彩虹'
    else if (val === 'Diamond Hill') val = '鑽石山'
    else if (val === 'Lok Fu') val = '樂富'
    else if (val === 'Ngau Chi Wan') val = '牛池灣'
    else if (val === 'San Po Kong') val = '新蒲崗'
    else if (val === 'Tsz Wan Shan') val = '慈雲山'
    else if (val === 'Wang Tau Hom') val = '橫頭磡'
    else if (val === 'Wong Tai Sin') val = '黃大仙'
    else if (val === 'Austin') val = '柯士甸'
    else if (val === 'Jordan') val = '佐敦'
    else if (val === 'Mong Kok') val = '旺角'
    else if (val === 'Prince Edward') val = '太子'
    else if (val === 'Tai Kok Tsui') val = '大角咀'
    else if (val === 'Tsim Sha Tsui') val = '尖沙咀'
    else if (val === 'Yau Ma Tei') val = '油麻地'
    else if (val === 'Ha Tsuen') val = '厦村'
    else if (val === 'Hung Shui Kiu') val = '洪水橋'
    else if (val === 'Kam Sheung Road') val = '錦上路'
    else if (val === 'Kam Tin') val = '錦田'
    else if (val === 'Lam Tsuen') val = '林村'
    else if (val === 'Lau Fau Shan') val = '流浮山'
    else if (val === 'Lok Ma Chau') val = '落馬洲'
    else if (val === 'Mai Po') val = '米埔'
    else if (val === 'Ngau Tam Mei') val = '牛潭尾'
    else if (val === 'Pat Heung') val = '八鄉'
    else if (val === 'Ping Shan') val = '屏山'
    else if (val === 'San Tin') val = '新田'
    else if (val === 'Shek Kong') val = '石崗'
    else if (val === 'Tai Tong') val = '大棠'
    else if (val === 'Tin Shui Wai') val = '天水圍'
    else if (val === 'Yuen Long') val = '元朗'
    else if (val === 'Peninsula de Macao"') val = '澳門半島'
    else if (val === 'Ilha Verde') val = '青洲'
    else if (val === 'Toi San') val = '台山區'
    else if (val === 'Areia Preta') val = '黑沙環'
    else if (val === 'Iao Hon') val = '祐漢'
    else if (val === 'Povoacao de Mong-Ha"') val = '望廈'
    else if (val === 'Fai Chi Kei') val = '筷子基'
    else if (val === 'Hipodromo') val = '馬場'
    else if (val === 'Lam Mau Tong') val = '林茂塘'
    else if (val === 'Costa') val = '高士德'
    else if (val === 'San Kiu') val = '新橋'
    else if (val === 'Zona de Aterros do Porto Exterior') val = '新口岸填海區'
    else if (val === 'Centro') val = '中區'
    else if (val === 'Colina da Guia') val = '松山'
    else if (val === 'Sao Lourenco') val = '下環'
    else if (val === 'Ilha da Taipa') val = '氹仔'
    else if (val === 'Taipa Island') val = '路氹城'
    else if (val === 'Coloane') val = '路環'
    else if (val === 'Vila De Coloane') val = '路環市區'
    else if (val === 'Seac Pai Va') val = '石排灣'
    else if (val === 'Povoacao de Ka Ho') val = '九澳村'
    else if (val === 'Povoacao Hac Sa') val = '黑沙村'
    else if (val === 'Macao') val = '澳門'
    else if (val === 'All Areas') val = '全部範圍';
    return val;
}

function translateType(val) {
    if (val === 'Convenience Store') val = '便利店';
    else if (val === 'Locker') val = '自提櫃';
    else if (val === 'Store') val = '自提點';
    else if (val === 'Post Office') val = '郵局';
    else if (val === 'Petrol Station') val = '油站';
    return val;
}