'use strict';
import { apiBaseGet } from '../../../../fd-toolbox/api/base-api.js';
import { getStringEnvSetting } from '../../../../fd-toolbox/infra/env-functions.js';
import { envSettingKeys } from '../../../common/infra/env-setting-keys.js';
import { GooglePlace } from '../../../common/leads/google-place.resource.js';
import { createError } from '../../../../fd-toolbox/errors/errors.js';
import { googlePlaceDetails } from './google-place-constants.js';
import { getNewUuid } from '../../../../fd-toolbox/uuids.js';
import { getResourcesWithOData } from '../../../meta/server/db/db.js';
import { singleOrUndefined } from '../../../../fd-toolbox/server/collections/single-funcs.js';
import { oDataOrderByDirections, oDataOperators } from '../../../../fd-toolbox/enums/enums.js';
import { resourceNames } from '../../../../fd-toolbox/resources/resource-names.js';
import { websiteScanResultProps } from '../../../common/resources/props/website-scan-result-props.js';
import { oDataComparisonTypes, oDataLogicalOperationTypes } from '../../../../fd-toolbox/odata/odata-enums.js';
import { baseResourceProps } from '../../../../fd-toolbox/base-resource/props/base-resource-props.js';
import { queryParamNames } from '../../constants/query-param-names.js';

var __defProp=Object['defineProperty'],__name=(_0x8e303,_0x24bcc4)=>__defProp(_0x8e303,'name',{'value':_0x24bcc4,'configurable':!![]});const placesAccessKey=getStringEnvSetting(envSettingKeys['googlePlacesApiKey']),baseTextSearchUrl='https://maps.googleapis.com/maps/api/place/textsearch/json',basePlaceDetailUrl='https://maps.googleapis.com/maps/api/place/details/json',fields='name,opening_hours,editorial_summary,formatted_address,formatted_phone_number,website,rating,review,user_ratings_total,place_id,photos,types';async function searchGooglePlaces(_0x7995ba,_0x2781fb=-0x16c1+-0xf1*0x1f+0x33f0){const _0x21742a=new URL(baseTextSearchUrl);_0x21742a['searchParams']['append'](queryParamNames['query'],_0x7995ba),_0x21742a['searchParams']['append'](queryParamNames['key'],placesAccessKey);const _0x5ae56b=await apiBaseGet(_0x21742a['toString']()),_0x372e1b=0x1010*0x1+0x1478+-0x2483,_0x5e2844=_0x5ae56b['results']['slice'](_0x2781fb,_0x2781fb+_0x372e1b),_0x2dbf76=[];for(const _0xcae918 of _0x5e2844['map'](_0x108bfd=>_0x108bfd['place_id'])){const _0x5d31ba=new URL(basePlaceDetailUrl);_0x5d31ba['searchParams']['append'](queryParamNames['fields'],fields),_0x5d31ba['searchParams']['append']('place_id',_0xcae918),_0x5d31ba['searchParams']['append'](queryParamNames['key'],placesAccessKey);try{const _0x12bf55=await apiBaseGet(_0x5d31ba['toString']()),_0x5396fc=await getResourcesWithOData(resourceNames['websiteScanResult'],{'filters':[{'leftSide':websiteScanResultProps['googlePlaceExternalId'],'operator':oDataOperators['equal'],'rightSide':_0x12bf55['result']['place_id'],'logicalOperationType':oDataLogicalOperationTypes['and'],'comparisonType':oDataComparisonTypes['equal']}],'orderBys':[{'propertyName':baseResourceProps['createdDate'],'direction':oDataOrderByDirections['asc']}],'top':0x1});_0x2dbf76['push'](createGooglePlaceFromGooglePlaceResponse(_0x12bf55,singleOrUndefined(_0x5396fc)));}catch{throw createError('Failed\x20to\x20get\x20place\x20details');}}return _0x2dbf76;}__name(searchGooglePlaces,'searchGooglePlaces');function createGooglePlaceFromGooglePlaceResponse(_0x344545,_0x1657f2){const _0x42827a=new GooglePlace();return _0x42827a['id']=getNewUuid(),_0x42827a['externalId']=_0x344545['result']['place_id'],_0x42827a['name']=_0x344545['result']['name']??'',_0x42827a['address']=_0x344545['result']['formatted_address']??'',_0x42827a['phoneNumber']=_0x344545['result']['formatted_phone_number']??'',_0x42827a['numberOfImages']=_0x344545['result']['photos']?.['length'],_0x42827a['workingHours']=_0x344545['result']['opening_hours']?.['weekday_text']['join'](','),_0x42827a['reviewRating']=Math['round'](_0x344545['result']['rating']??-0x1b74+0x3*0x1be+0x163a),_0x42827a['website']=_0x344545['result']['website'],_0x42827a['claimed']=_0x344545['result']['business_status']===googlePlaceDetails['operational'],_0x42827a['websiteScanResult']=_0x1657f2,_0x42827a['emails']=_0x1657f2?.['emails']?.['split'](','),_0x42827a;}__name(createGooglePlaceFromGooglePlaceResponse,'createGooglePlaceFromGooglePlaceResponse');

export { searchGooglePlaces };
