/*************************************************************************
* ADOBE CONFIDENTIAL
* ___________________
*
*  Copyright 2015 Adobe Systems Incorporated
*  All Rights Reserved.
*
* NOTICE:  All information contained herein is, and remains
* the property of Adobe Systems Incorporated and its suppliers,
* if any.  The intellectual and technical concepts contained
* herein are proprietary to Adobe Systems Incorporated and its
* suppliers and are protected by all applicable intellectual property laws,
* including trade secret and or copyright laws.
* Dissemination of this information or reproduction of this material
* is strictly forbidden unless prior written permission is obtained
* from Adobe Systems Incorporated.
**************************************************************************/
import state from"./state.js";import{getClosestElementBasedOnSelector,LIST_VIEW,getElementsByClassNameSelectors,getAttachmentByFileTypeSelectors,ACROBAT_NON_PDF_PROCESSED}from"./util.js";import{addCommonClickListener,markElementAsProcessed}from"./gmail-verb-native-viewer.js";import{getAttachmentDetailsFromThreadData,getVisibleListView,isMultipleAttachmentWithSameName,shouldShowTouchPoint}from"./list-view-touch-point-service.js";const getListViewAttachmentsWithThreadElement=(t,e,s)=>{const o=getElementsByClassNameSelectors(t,"attachmentIcon","listView");if(0===o?.length)return;const a=getAttachmentByFileTypeSelectors(o,state?.gmailConvertToPdfConfig?.metadata?.selectors,ACROBAT_NON_PDF_PROCESSED);if(!a?.length)return{};const n={};for(const t of a){const o=getClosestElementBasedOnSelector(t,"threadElement","listView"),a=o?.querySelector("[data-thread-id]");if(!a)continue;const r=a.getAttribute("data-thread-id"),i=r?.substring(r?.indexOf("#")+1);isDataPresentForThreadId(i,e)&&(n[i]?n[i][s].push(t):n[i]={[s]:[t],threadElement:o})}return n};function mergeAttachments(t,e){if(e)for(const[s,o]of Object.entries(e)){t[s]||(t[s]=[]);const e=o.map(({url:t})=>t);t[s].push(...e)}}const getAttachmentURLAgainstName=t=>{const e={},s=Object.values(state?.gmailConvertToPdfConfig?.metadata?.fileExtToMimeTypeMap||{});for(const o of Object.values(t)){const{nonPDFAttachments:t,driveAttachments:a}=o;for(const n of s)t?.size>0&&mergeAttachments(e,t.get(n)),a?.size>0&&mergeAttachments(e,o.nonPDFDriveAttachments.get(n))}return e},isDataPresentForThreadId=(t,e)=>{let s=state.getMessagesForThreadId(t);return!s&&e&&e[t]&&(s=e[t].messages),!!s},getDataForThreadId=(t,e)=>{let s=state.getMessagesForThreadId(t);return!s&&e?.[t]&&(s=e[t].messages),s?getAttachmentURLAgainstName(s):null},processForAttachment=(t,e)=>{if(!e||0===Object.keys(e).length)return;const s=getClosestElementBasedOnSelector(t,"attachmentDiv","listView");if(s&&"Y"!==t.getAttribute(ACROBAT_NON_PDF_PROCESSED)){markElementAsProcessed(t);const o=s.getAttribute("title"),a=isMultipleAttachmentWithSameName(e,o),n=!a&&e[o]?.[0];!a&&shouldShowTouchPoint(n)&&addCommonClickListener(s,{name:o,url:n},LIST_VIEW,state?.eventControllerSignal)}},processForThread=(t,e,s,o)=>{const a=s[o],n=getDataForThreadId(t,e);if(n)for(const t of a)processForAttachment(t,n)},processForAllThreads=(t,e,s)=>{for(const[o,a]of Object.entries(t)){const t=a[s];t?.length>0&&processForThread(o,e,a,s)}},addTouchPointInNativeViewerViaListView=(t,e,s)=>{const o=getListViewAttachmentsWithThreadElement(t,e,s);o&&processForAllThreads(o,e,s)},addTouchpointToNativeViewerViaListView=t=>{try{if(!chrome?.runtime?.id||!state?.gmailConvertToPdfConfig?.enableConvertToPdfTouchpointInGmail)return;const e=getVisibleListView();e&&addTouchPointInNativeViewerViaListView(e,t,"nonPdfAttachments")}catch(t){}};export{addTouchpointToNativeViewerViaListView};