'use strict';
import { getOData } from '../../meta/server/repositories/odata-db.js';
import { resourceNames } from '../../../fd-toolbox/resources/resource-names.js';
import { shippoProviderName, fedexProviderName, fedexTestApi, fedexRoutes, fedexLabels, shippoTestTrackingNumber, shippoCarrier, shippoApiUrl, shippoLabels } from './shipment-constants.js';
import { getResourceMeta } from '../../meta/server/services/metas.js';
import { shipmentTrackingStatuses } from '../../common/shipment/shipment-tracking-status.js';
import { getRequiredStringEnvSetting } from '../../../fd-toolbox/infra/env-functions.js';
import { metaEnvSettingKeys } from '../../meta/infra/meta-env-setting-keys.js';
import { logError } from '../../../fd-toolbox/server/logging/logger.js';
import { postBaseApi } from '../../../fd-toolbox/api/base-api.js';
import { httpContentTypes, headerNames } from '../../../fd-toolbox/constants/header-names.js';
import { getFedexToken } from './shipment-api.js';
import { first } from '../../../fd-toolbox/server/collections/single-funcs.js';

var __defProp=Object['defineProperty'],__name=(_0x2bff77,_0xf3f714)=>__defProp(_0x2bff77,'name',{'value':_0xf3f714,'configurable':!![]});async function get(_0x4804d5){const _0x2fd23b=await getResourceMeta(resourceNames['shipment']),_0x2e8794=await getOData(_0x2fd23b['resourceName'],_0x2fd23b['orderBys'],_0x4804d5);for(const _0x4613e7 of _0x2e8794){await getShipmentLabel(_0x4613e7);}return _0x2e8794;}__name(get,'get');async function getShipmentLabel(_0x172363){if(_0x172363['shipmentProvider'])try{if(_0x172363['shipmentProvider']['name']===shippoProviderName)await fetchShippoLabel(_0x172363);else {if(_0x172363['shipmentProvider']['name']===fedexProviderName)await fetchFedexLabel(_0x172363);}}catch(_0x490f49){_0x490f49 instanceof Error&&logError(_0x490f49['message']);}return _0x172363;}__name(getShipmentLabel,'getShipmentLabel');async function fetchFedexLabel(_0x4fc80f){const _0x264384={'trackingInfo':[{'trackingNumberInfo':{'trackingNumber':_0x4fc80f['trackingNumber']}}],'includeDetailedScans':!![]},_0x157b18=new Headers({}),_0x4e7cdd=await getFedexToken();_0x157b18['set'](httpContentTypes['contentTypeHeaderName'],httpContentTypes['contentTypeApplicationJson']),_0x157b18['set'](headerNames['authHeaderName'],headerNames['authHeaderValuePrefix']+_0x4e7cdd);const _0x466ea2=await postBaseApi(fedexTestApi+fedexRoutes['trackNum'],_0x264384,_0x157b18),_0x4646df=first(first(_0x466ea2['output']['completeTrackResults'])['trackResults'])['latestStatusDetail']['code'];_0x4fc80f['trackingStatus']=formatFedexLabel(_0x4646df);}__name(fetchFedexLabel,'fetchFedexLabel');function formatFedexLabel(_0x498d7d){switch(_0x498d7d){case fedexLabels['delivered']:return shipmentTrackingStatuses['delivered'];case fedexLabels['shipped']:return shipmentTrackingStatuses['shipped'];case fedexLabels['outForDelivery']:return shipmentTrackingStatuses['outForDelivery'];default:return shipmentTrackingStatuses['notShipped'];}}__name(formatFedexLabel,'formatFedexLabel');async function fetchShippoLabel(_0xe96f61){const _0x435267=new URLSearchParams({'carrier':shippoCarrier,'tracking_number':shippoTestTrackingNumber}),_0x47d5ac=new Headers();_0x47d5ac['set'](httpContentTypes['contentTypeHeaderName'],httpContentTypes['contentTypeApplicationForm']),_0x47d5ac['set'](headerNames['authHeaderName'],headerNames['shippoAuthPrefix']+getRequiredStringEnvSetting(metaEnvSettingKeys['shippoApiKey']));const _0x11db54=await postBaseApi(shippoApiUrl,_0x435267,_0x47d5ac);_0xe96f61['trackingStatus']=formatShippoLabel(_0x11db54['tracking_status']['status']);}__name(fetchShippoLabel,'fetchShippoLabel');function formatShippoLabel(_0x12faa4){switch(_0x12faa4){case shippoLabels['notShipped']:return shipmentTrackingStatuses['notShipped'];case shippoLabels['shipped']:return shipmentTrackingStatuses['shipped'];case shippoLabels['delivered']:return shipmentTrackingStatuses['delivered'];default:return shipmentTrackingStatuses['notShipped'];}}__name(formatShippoLabel,'formatShippoLabel');

export { get as getShipment };
