{"version":3,"file":"export_report-DI8Jk67Y.js","names":["__toESM","import_toastify","require_toastify","initReportExport","Alpine","config","params","form","formData","key","value","format","token","preflightParams","pfUrl","res","data","errMsg","downloadParams","downloadUrl","link","err","maxWaitTime","startTime","match","c","cookieVal"],"sources":["../../../frontend/application/dashboards/export_report.js"],"sourcesContent":["/**\n * Reusable Report Export Alpine.js Controller for Aurora CRM\n *\n * Coordinates non-blocking native streaming downloads, pre-flight validation,\n * double-click prevention, and cookie-based completion synchronization.\n */\nimport Toastify from \"toastify-js\";\n\nexport function initReportExport(Alpine) {\n  Alpine.data(\"reportExport\", (config = {}) => ({\n    open: false,\n    isExporting: false,\n    activeFormat: \"\",\n    preflightUrl: config.preflightUrl || \"\",\n    exportUrl: config.exportUrl || \"\",\n    formId: config.formId || \"time-tracking-filter-form\",\n    pollInterval: null,\n    timeoutTimer: null,\n\n    getParams(format, token = null) {\n      const params = new URLSearchParams();\n      const form = document.getElementById(this.formId);\n\n      if (form) {\n        const formData = new FormData(form);\n        for (const [key, value] of formData.entries()) {\n          if (value !== null && value !== \"\") {\n            params.append(key, value);\n          }\n        }\n      }\n\n      params.set(\"format\", format);\n      if (token) {\n        params.set(\"download_token\", token);\n      }\n      return params;\n    },\n\n    async triggerExport(format) {\n      if (this.isExporting) return;\n\n      this.isExporting = true;\n      this.activeFormat = format;\n\n      const preflightParams = this.getParams(format);\n\n      try {\n        const pfUrl = `${this.preflightUrl}?${preflightParams.toString()}`;\n        const res = await fetch(pfUrl, {\n          headers: {\n            \"X-Requested-With\": \"XMLHttpRequest\",\n          },\n        });\n\n        const data = await res.json().catch(() => ({}));\n\n        if (!res.ok || data.ok === false) {\n          const errMsg = data.error || \"Export failed safety validation.\";\n          Toastify({\n            text: errMsg,\n            duration: 6000,\n            close: true,\n            gravity: \"top\",\n            position: \"right\",\n            style: {\n              background: \"#e11d48\",\n              borderRadius: \"0.75rem\",\n              fontSize: \"0.8125rem\",\n              fontWeight: \"600\",\n              boxShadow: \"0 10px 15px -3px rgba(0, 0, 0, 0.1)\",\n            },\n          }).showToast();\n\n          this.resetExportState();\n          return;\n        }\n\n        // Preflight passed -> execute native stream with cookie handshake\n        const token = `${Date.now()}_${Math.random().toString(36).substring(2, 8)}`;\n        const downloadParams = this.getParams(format, token);\n        const downloadUrl = `${this.exportUrl}?${downloadParams.toString()}`;\n\n        this.startCookiePolling(token);\n\n        // Trigger native browser download stream\n        const link = document.createElement(\"a\");\n        link.href = downloadUrl;\n        link.setAttribute(\"download\", \"\");\n        link.style.display = \"none\";\n        document.body.appendChild(link);\n        link.click();\n        document.body.removeChild(link);\n      } catch (err) {\n        console.error(\"Export error:\", err);\n        Toastify({\n          text: \"An unexpected network error occurred while preparing your export.\",\n          duration: 5000,\n          close: true,\n          gravity: \"top\",\n          position: \"right\",\n          style: {\n            background: \"#e11d48\",\n            borderRadius: \"0.75rem\",\n            fontSize: \"0.8125rem\",\n            fontWeight: \"600\",\n          },\n        }).showToast();\n        this.resetExportState();\n      }\n    },\n\n    startCookiePolling(token) {\n      const pollFrequency = 150;\n      const maxWaitTime = 40000; // 40s safety timeout\n      const startTime = Date.now();\n\n      if (this.pollInterval) clearInterval(this.pollInterval);\n      if (this.timeoutTimer) clearTimeout(this.timeoutTimer);\n\n      this.pollInterval = setInterval(() => {\n        const cookies = document.cookie.split(\";\").map((c) => c.trim());\n        const match = cookies.find((c) => c.startsWith(\"crm_download_complete=\"));\n\n        if (match) {\n          const cookieVal = match.split(\"=\")[1];\n          if (cookieVal === token || cookieVal) {\n            // Clean up cookie immediately\n            document.cookie = \"crm_download_complete=; Max-Age=0; path=/;\";\n            this.handleDownloadSuccess();\n          }\n        }\n\n        if (Date.now() - startTime > maxWaitTime) {\n          this.resetExportState();\n        }\n      }, pollFrequency);\n\n      this.timeoutTimer = setTimeout(() => {\n        this.resetExportState();\n      }, maxWaitTime);\n    },\n\n    handleDownloadSuccess() {\n      this.resetExportState();\n      Toastify({\n        text: `Report exported successfully (${this.activeFormat.toUpperCase()}).`,\n        duration: 3500,\n        close: true,\n        gravity: \"top\",\n        position: \"right\",\n        style: {\n          background: \"#059669\",\n          borderRadius: \"0.75rem\",\n          fontSize: \"0.8125rem\",\n          fontWeight: \"600\",\n          boxShadow: \"0 10px 15px -3px rgba(0, 0, 0, 0.1)\",\n        },\n      }).showToast();\n    },\n\n    resetExportState() {\n      if (this.pollInterval) {\n        clearInterval(this.pollInterval);\n        this.pollInterval = null;\n      }\n      if (this.timeoutTimer) {\n        clearTimeout(this.timeoutTimer);\n        this.timeoutTimer = null;\n      }\n      this.isExporting = false;\n      this.activeFormat = \"\";\n    },\n  }));\n}\n"],"mappings":"A,0DAQA,SAAgBG,EAAiB,EAAQ,CACvCC,EAAO,KAAK,gBAAiB,EAAS,CAAC,KAAO,CAC5C,KAAM,GACN,YAAa,GACb,aAAc,GACd,aAAcC,EAAO,cAAgB,GACrC,UAAWA,EAAO,WAAa,GAC/B,OAAQA,EAAO,QAAU,4BACzB,aAAc,KACd,aAAc,KAEd,UAAU,EAAQ,EAAQ,KAAM,CAC9B,IAAMC,EAAS,IAAI,gBACbC,EAAO,SAAS,eAAe,KAAK,MAAM,EAEhD,GAAIA,EAAM,CACR,IAAMC,EAAW,IAAI,SAASD,CAAI,EAClC,IAAK,GAAM,CAACE,EAAKC,KAAUF,EAAS,QAAQ,EACtCE,IAAU,MAAQA,IAAU,IAC9BJ,EAAO,OAAOG,EAAKC,CAAK,CAG9B,CAMA,OAJAJ,EAAO,IAAI,SAAUK,CAAM,EACvBC,GACFN,EAAO,IAAI,iBAAkBM,CAAK,EAE7BN,CACT,EAEA,MAAM,cAAc,EAAQ,CAC1B,GAAI,KAAK,YAAa,OAEtB,KAAK,YAAc,GACnB,KAAK,aAAeK,EAEpB,IAAME,EAAkB,KAAK,UAAUF,CAAM,EAE7C,GAAI,CACF,IAAMG,EAAQ,GAAG,KAAK,aAAa,GAAGD,EAAgB,SAAS,IACzDE,EAAM,MAAM,MAAMD,EAAO,CAC7B,QAAS,CACP,mBA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