'use strict';
import { screenshotConstants } from '../../../common/fd/media/screenshot-constants.js';
import { apiBadRequestError } from '../../../../fd-toolbox/server/web/errors.js';
import { apiOkResponse } from '../../../meta/server/web/responses.js';
import { parseJsonBodyFromRequest } from '../../../../fd-toolbox/lib/utils.js';
import { crudCreate, crudUpdate } from '../../../meta/server/basic-crud/cruds.js';
import { resourceNames } from '../../../../fd-toolbox/resources/resource-names.js';
import { getUserId } from '../../../meta/server/auth/users/current-user-id-provider.js';
import { fillWorkspaceResourceProps } from '../../../fd-app-toolbox/server/base-resources/props/fill-workspace-resource-props.js';
import { Blob } from '../../../meta/core/resources/blob/blob.resource.js';
import { metaErrorMessages } from '../../../meta/constants/meta-error-messages-constants.js';
import { createError } from '../../../../fd-toolbox/errors/errors.js';
import { screenshotNotifications } from './screenshot-constants.js';
import { getResource } from '../../../meta/server/db/db.js';
import { captureWebsiteScreenshot } from './get-website-screenshots.js';

var __defProp=Object['defineProperty'],__name=(_0x368116,_0x1fa5de)=>__defProp(_0x368116,'name',{'value':_0x1fa5de,'configurable':!![]});async function postScreenshot(_0x3d5d5b){const _0x4b81d2=await parseJsonBodyFromRequest(_0x3d5d5b),_0x260656=[];for(const _0x450586 of _0x4b81d2['idAndWebsiteUrls']){try{const _0x18b089=screenshotConstants['urlRegex'];if(!_0x18b089['exec'](_0x450586['websiteUrl']))throw apiBadRequestError(screenshotConstants['invalidUrlFormatMessage']);const _0x1c9e01=new Blob(),_0x27cbf7=await getUserId(),_0x504ebc=await fillWorkspaceResourceProps(_0x1c9e01);_0x504ebc['blobUrl']=String(await captureWebsiteScreenshot(_0x450586['websiteUrl'])),assignResourceIdToBlobConfig(_0x504ebc,_0x4b81d2['resourceName'],_0x450586['id']);const _0x2eb5f7=await crudCreate({'resourceName':resourceNames['blob'],'resource':_0x504ebc,'userId':_0x27cbf7,'throwOnNonSuccess':![],'isCalledFromOutside':!![]});if(_0x4b81d2['resourceName']!==resourceNames['googlePlace']){const _0x3636c7=await getResource({'resourceName':_0x4b81d2['resourceName'],'id':_0x450586['id']});_0x2eb5f7['result']?.['id']&&await crudUpdate({'resourceName':_0x4b81d2['resourceName'],'resource':{..._0x3636c7,'screenshotBlobId':_0x2eb5f7['result']['id']},'userId':_0x27cbf7});}_0x260656['push']({'id':_0x450586['id'],'isSuccess':!![]});}catch{_0x260656['push']({'id':_0x450586['id'],'message':screenshotNotifications['takeFailed'],'isSuccess':![]});}}return apiOkResponse(_0x260656);}__name(postScreenshot,'postScreenshot');function assignResourceIdToBlobConfig(_0x8e00fc,_0x3a01b0,_0x2382ab){if(_0x3a01b0===resourceNames['googlePlace'])_0x8e00fc['googlePlaceExternalId']=_0x2382ab;else {if(_0x3a01b0===resourceNames['lead'])_0x8e00fc['leadId']=_0x2382ab;else throw createError(metaErrorMessages['unsupportedResourceName']['replace']('{0}',_0x3a01b0));}}__name(assignResourceIdToBlobConfig,'assignResourceIdToBlobConfig');

export { postScreenshot };
