/*************************************************************************
* 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{sendAnalyticsOncePerMonth,getElementListForSelectors}from"../utils/util.js";const FILE_DATA_ID_ATTRIBUTE="data-id",GDRIVE_TOUCH_POINT_CLASS="acrobat-gdrive-touch-point",GDRIVE_PDF_ELEMENT_PROCESSED="acrobat-processed",GDRIVE_PDF_ELEMENT_EXCLUDED="acrobat-excluded",checkDivImageType=(e,t)=>{const i=e?.getElementsByTagName("path");if(i&&i.length>0){const e=i[0]?.getAttribute("d");if(t?.includes(e))return!0}return!1},isFileElementSelected=e=>{const t="true"===e.getAttribute("aria-selected"),i="true"===e?.childNodes[0]?.getAttribute("aria-selected");return t||i},getSelectedFiles=e=>Array.from(e).filter(e=>isFileElementSelected(e)),isListViewTouchPointEnabled=()=>state?.config?.enableGDriveListViewTouchPoint&&state?.config?.selectors?.GoogleDriveListView,isGridViewTouchPointEnabled=()=>state?.config?.enableGDriveGridViewTouchPoint&&state?.config?.selectors?.GoogleDriveGridView,getEligiblePdfFileElementList=(e,t,i)=>e.filter(e=>{if(e.hasAttribute("acrobat-excluded"))return!1;const r=0===e.getElementsByClassName(GDRIVE_TOUCH_POINT_CLASS)?.length,l=checkDivImageType(e,t),s=getElementListForSelectors(i?.exclude,e)?.length>0;return r&&l&&!s?(e.setAttribute("acrobat-processed",!0),!0):(l&&s?sendAnalyticsOncePerMonth(`DCBrowserExt:GDrive:${state?.selectedView}:PdfFileNotSupported`,{eventContext:state?.driveUrlPath}):l&&!s||e.setAttribute("acrobat-excluded","true"),!1)}),setDrivePath=()=>{const e=window.location.pathname;if(""!==state?.driveUrlPath&&state?.prevDriveUrlPath===e)return state?.driveUrlPath;const t=e?.split("/");return t?.length>=3&&(t?.length>=5&&"u"===t[2]?state.driveUrlPath=t[4]:t?.length>=3&&(state.driveUrlPath=t[2])),state.prevDriveUrlPath=e,state?.driveUrlPath},getSelectorsForCurrentPathAndView=e=>{const t=state?.config?.selectors[e];let i=state?.driveUrlPath;return t?.pathSelector?(t?.pathSelector[i]||(i="default"),t?.pathSelector[i]):{fileElement:[],fileTitle:[],touchpointContainer:[]}},getSelectorsForCurrentPathAndSelectedView=()=>getSelectorsForCurrentPathAndView(state?.selectedView),checkViewByElementWidth=(e,t)=>{const i=getSelectorsForCurrentPathAndView(e),r=getElementListForSelectors(i?.fileElement);return{isView:r?.some(e=>0!==e.getBoundingClientRect().width&&t(e.getBoundingClientRect().width)),canBeView:r?.length>0}},GOOGLE_DRIVE_LIST_VIEW="GoogleDriveListView",GOOGLE_DRIVE_GRID_VIEW="GoogleDriveGridView",setSelectedView=()=>{const{isView:e,canBeView:t}=isListViewTouchPointEnabled?checkViewByElementWidth("GoogleDriveListView",e=>e>600):{isListView:!1,canBeListView:!1};if(e)state.selectedView="GoogleDriveListView";else{const{isView:e,canBeView:i}=isGridViewTouchPointEnabled()?checkViewByElementWidth("GoogleDriveGridView",e=>e<600):{isGridView:!1,canBeGridView:!1};e?state.selectedView="GoogleDriveGridView":t?state.selectedView="GoogleDriveListView":i&&(state.selectedView="GoogleDriveGridView")}},createUrlForAcrobatTouchPoint=(e,t)=>{const i=`https://drive.usercontent.google.com/download?id=${e?.id}&authuser=${state?.userId}&acrobatPromotionSource=${t}`;return chrome.runtime.getURL("viewer.html")+"?pdfurl="+encodeURIComponent(i)+"&pdffilename="+encodeURIComponent(e?.title)},createCustomClassNameBasedOnViewAndPath=(e,t,i,r,l)=>"GoogleDriveListView"===t?`${e}-${t}`:"GoogleDriveGridView"===t?"home"===i?`${e}-${t}-${i}`:r===l?`${e}-${t}-pdfFileHost`:"recent"===i?`${e}-${t}-recent`:`${e}-${t}`:"",getPdfFileDetails=(e,t)=>{const i={id:"",title:""},r=e?.getAttribute("data-id"),l=getElementListForSelectors(t?.fileTitle,e);if(r&&(i.id=r),l?.length>0){const e=l[0]?.innerText;e&&isFileTypePdfInTitle(e)&&(i.title=e)}return i},isFileTypePdfInTitle=e=>e.endsWith(".pdf")||e.endsWith(".PDF"),isEventTargetWithinElement=(e,t)=>!!t&&t.contains(e?.target),areFilesPdfs=e=>{if(!e)return!1;const t=Array.isArray(e)?e:[e];if(0===t.length)return!1;const i=state?.config?.selectors[state?.selectedView]?.pdfSvgPath;return t.every(e=>checkDivImageType(e,i))},buildClassSelectorString=e=>e?e.map(e=>`.${e}`).join(","):"",areFileDetailsValid=e=>""!==e?.id&&""!==e?.title,isKeyboardEventOnFileElement=e=>e?.target?.hasAttribute("data-id")||e?.target?.parentElement?.hasAttribute("data-id");export{createUrlForAcrobatTouchPoint,createCustomClassNameBasedOnViewAndPath,getEligiblePdfFileElementList,getSelectorsForCurrentPathAndSelectedView,getSelectedFiles,getPdfFileDetails,setDrivePath,setSelectedView,isFileElementSelected,isEventTargetWithinElement,areFilesPdfs,buildClassSelectorString,isFileTypePdfInTitle,areFileDetailsValid,isKeyboardEventOnFileElement};