(()=>{"use strict";var e,r={1455(e){e.exports=window.wp.apiFetch},4778(e,r,s){var o=s(1455),t=s.n(o);const l="__puffergoApiFetchErrorTelemetryInstalled";"undefined"==typeof window||window[l]||(window[l]=!0,t().use((e,r)=>{const s=r(e);return s&&"function"==typeof s.then?s.catch(r=>{try{!function(e){var r,s,o,t;if("undefined"==typeof window||"undefined"==typeof fetch)return;const l=window.puffergoTelemetry,n=l&&"string"==typeof l.baseUrl?l.baseUrl:"https://api.puffergo.com",a=l&&"object"==typeof l.env&&null!==l.env?l.env:{},i=(n+"").replace(/\/$/,"")+"/api/telemetry/event",p=JSON.stringify({eventType:"error",domain:null!==(r=a.domain)&&void 0!==r?r:null,phpVersion:null!==(s=a.phpVersion)&&void 0!==s?s:null,wpVersion:null!==(o=a.wpVersion)&&void 0!==o?o:null,pufferGoVersion:null!==(t=a.pufferGoVersion)&&void 0!==t?t:null,content:JSON.stringify(e)});try{fetch(i,{method:"POST",headers:{"Content-Type":"application/json"},body:p,credentials:"omit",keepalive:!0,mode:"cors"}).catch(()=>{})}catch{}}(function(e,r){const s="string"==typeof e?.path?e.path:"string"==typeof e?.url?e.url:"",o="string"==typeof e?.method?e.method:"GET";let t=null;r&&"object"==typeof r&&r.data&&"number"==typeof r.data.status?t=r.data.status:r&&"object"==typeof r&&"number"==typeof r.status&&(t=r.status);const l=r&&"object"==typeof r&&"string"==typeof r.code?r.code:null;let n="";return r instanceof Error?n=r.message||"":r&&"object"==typeof r&&"string"==typeof r.message?n=r.message:null!=r&&(n=r+""),{source:"wp_plugin_js",restPath:s,restMethod:o,httpStatus:t,code:l,message:n.slice(0,2048)}}(e,r))}catch{}return Promise.reject(r)}):s}))},7122(e,r,s){const o=window.wp.element;var t=s(1455),l=s.n(t);const n=window.wp.i18n,a=window.wp.components,i=window.ReactJSXRuntime,p=[{id:1,label:(0,n.__)("Upload","puffergo")},{id:2,label:(0,n.__)("Review","puffergo")},{id:3,label:(0,n.__)("Done","puffergo")}],d=({currentStep:e})=>(0,i.jsx)("ol",{className:"pgo-import-stepper",children:p.map(r=>{const s=r.id===e?"is-current":r.id<e?"is-done":"is-upcoming";return(0,i.jsxs)("li",{className:"pgo-import-stepper-item "+s,children:[(0,i.jsx)("span",{className:"pgo-import-stepper-index",children:r.id}),(0,i.jsx)("span",{className:"pgo-import-stepper-label",children:r.label})]},r.id)})});function c(e,{emptyLabel:r}={}){return!e||Array.isArray(e)||"object"!=typeof e?null!=r?r:"—":"contact"===e.type?(0,n.__)("Contact for price","puffergo"):void 0!==e.min&&void 0!==e.max?`${e.min}–${e.max}${e.unit?" "+e.unit:""}`:void 0!==e.value?`${e.value}${e.unit?" "+e.unit:""}`:null!=r?r:"—"}const u=[".xlsx",".csv"],m=({config:e,parsing:r,parseError:s,onFileSelected:t,onClearError:l})=>{const p=(0,o.useRef)(null),[d,c]=(0,o.useState)(!1),[m,f]=(0,o.useState)(null),g=e.maxFileSize||0,h=e.maxRows||0,x=(0,o.useCallback)(e=>{e&&(function(e){const r=(e||"").toLowerCase();return u.some(e=>r.endsWith(e))}(e.name)?g&&e.size>g?f((0,n.sprintf)(/* translators: %s: maximum allowed file size */ /* translators: %s: maximum allowed file size */
(0,n.__)("File is too large. Maximum size is %s.","puffergo"),function(e){if(!e&&0!==e)return"";if(1024>e)return e+" B";let r=e/1024,s=0;for(;r>=1024&&2>s;)r/=1024,s+=1;return`${r.toFixed(1)} ${["KB","MB","GB"][s]}`}(g))):(f(null),l?.(),t(e)):f((0,n.__)("Please choose an .xlsx or .csv file.","puffergo")))},[g,t,l]),_=()=>{r||p.current?.click()},j=s||m;return(0,i.jsxs)("div",{className:"pgo-import-upload-step",children:[(0,i.jsxs)("div",{className:"pgo-import-upload-row",children:[(0,i.jsxs)("div",{className:`pgo-import-dropzone${d?" is-dragover":""}${r?" is-busy":""}`,onClick:_,onKeyDown(e){"Enter"!==e.key&&" "!==e.key||_()},role:"button",tabIndex:0,onDrop(e){e.preventDefault(),c(!1);const r=e.dataTransfer.files?.[0];x(r)},onDragOver(e){e.preventDefault(),c(!0)},onDragLeave(e){e.preventDefault(),c(!1)},children:[(0,i.jsx)("input",{ref:p,type:"file",accept:".xlsx,.csv",className:"pgo-import-file-input",onChange(e){const r=e.target.files?.[0];x(r),e.target.value=""},disabled:r}),r?(0,i.jsxs)("div",{className:"pgo-import-dropzone-content",children:[(0,i.jsx)(a.Spinner,{}),(0,i.jsx)("p",{children:(0,n.__)("Parsing file…","puffergo")})]}):(0,i.jsxs)("div",{className:"pgo-import-dropzone-content",children:[(0,i.jsx)("span",{className:"dashicons dashicons-upload","aria-hidden":"true"}),(0,i.jsx)("p",{className:"pgo-import-dropzone-title",children:(0,n.__)("Drag and drop your file here, or click to choose one","puffergo")}),(0,i.jsx)("p",{className:"pgo-import-dropzone-hint",children:(0,n.__)("Accepted formats: .xlsx, .csv","puffergo")})]})]}),(0,i.jsx)("div",{className:"pgo-import-upload-side",children:(0,i.jsx)(a.Button,{variant:"secondary",href:e.templateUrl,disabled:!e.templateUrl,children:(0,n.__)("Download Template","puffergo")})})]}),j&&(0,i.jsx)(a.Notice,{status:"error",isDismissible:!!m,onRemove(){return f(null)},children:j}),(0,i.jsx)("p",{className:"pgo-import-help-text",children:(0,n.sprintf)(/* translators: %d: maximum number of rows supported per import */ /* translators: %d: maximum number of rows supported per import */
(0,n.__)("You can import up to %d products at a time. Imported products are created as drafts so you can review them before publishing.","puffergo"),h)})]})},f=({row:e})=>e.title&&!1!==e.valid?e.duplicate?(0,i.jsx)("span",{className:"pgo-import-badge pgo-import-badge-duplicate",children:(0,n.__)("Exists","puffergo")}):e.errors&&e.errors.length?(0,i.jsx)("span",{className:"pgo-import-badge pgo-import-badge-warning",children:(0,n.__)("Check","puffergo")}):(0,i.jsx)("span",{className:"pgo-import-badge pgo-import-badge-ready",children:(0,n.__)("Ready","puffergo")}):(0,i.jsx)("span",{className:"pgo-import-badge pgo-import-badge-invalid",children:(0,n.__)("Invalid","puffergo")}),g=e=>!!e.title&&!1!==e.valid,h=({rows:e,summary:r,selected:s,onToggleRow:t,onToggleAll:l,overwriteDuplicates:p,onOverwriteChange:d,onBack:u,onImport:m,importing:h})=>{var x,_,j,v;const[w,b]=(0,o.useState)(null),y=(0,o.useMemo)(()=>e.filter(g),[e]),N=y.length>0&&y.every(e=>s.has(e.row_number)),S=(0,o.useMemo)(()=>e.some(e=>e.duplicate&&s.has(e.row_number)),[e,s]),k=s.size;return(0,i.jsxs)("div",{className:"pgo-import-review-step",children:[(0,i.jsxs)("div",{className:"pgo-import-summary-bar",children:[(0,i.jsxs)("div",{className:"pgo-import-summary-item",children:[(0,i.jsx)("span",{className:"pgo-import-summary-value",children:null!==(x=r.total)&&void 0!==x?x:e.length}),(0,i.jsx)("span",{className:"pgo-import-summary-label",children:(0,n.__)("Total","puffergo")})]}),(0,i.jsxs)("div",{className:"pgo-import-summary-item is-valid",children:[(0,i.jsx)("span",{className:"pgo-import-summary-value",children:null!==(_=r.valid)&&void 0!==_?_:0}),(0,i.jsx)("span",{className:"pgo-import-summary-label",children:(0,n.__)("Valid","puffergo")})]}),(0,i.jsxs)("div",{className:"pgo-import-summary-item is-invalid",children:[(0,i.jsx)("span",{className:"pgo-import-summary-value",children:null!==(j=r.invalid)&&void 0!==j?j:0}),(0,i.jsx)("span",{className:"pgo-import-summary-label",children:(0,n.__)("Invalid","puffergo")})]}),(0,i.jsxs)("div",{className:"pgo-import-summary-item is-duplicate",children:[(0,i.jsx)("span",{className:"pgo-import-summary-value",children:null!==(v=r.duplicates)&&void 0!==v?v:0}),(0,i.jsx)("span",{className:"pgo-import-summary-label",children:(0,n.__)("Duplicates","puffergo")})]})]}),(0,i.jsx)("div",{className:"pgo-import-table-toolbar",children:(0,i.jsx)(a.CheckboxControl,{label:(0,n.__)("Select all eligible rows","puffergo"),checked:N,onChange(e){return l(e)},disabled:0===y.length})}),(0,i.jsx)("div",{className:"pgo-import-table-wrap",children:(0,i.jsxs)("table",{className:"pgo-import-table widefat striped",children:[(0,i.jsx)("thead",{children:(0,i.jsxs)("tr",{children:[(0,i.jsx)("td",{className:"pgo-import-col-checkbox"}),(0,i.jsx)("th",{children:(0,n.__)("Row","puffergo")}),(0,i.jsx)("th",{children:(0,n.__)("Product Name","puffergo")}),(0,i.jsx)("th",{children:(0,n.__)("Categories","puffergo")}),(0,i.jsx)("th",{children:(0,n.__)("Price","puffergo")}),(0,i.jsx)("th",{children:(0,n.__)("MOQ","puffergo")}),(0,i.jsx)("th",{children:(0,n.__)("Specs","puffergo")}),(0,i.jsx)("th",{children:(0,n.__)("Images","puffergo")}),(0,i.jsx)("th",{children:(0,n.__)("Status","puffergo")})]})}),(0,i.jsx)("tbody",{children:e.map(e=>{const r=g(e),l=s.has(e.row_number),p=e.specs||[],d=p.slice(0,2),u=p.length-d.length,m=!(!e.errors||!e.errors.length),h=w===e.row_number;return(0,i.jsxs)(o.Fragment,{children:[(0,i.jsxs)("tr",{className:r?"":"is-disabled-row",children:[(0,i.jsx)("td",{className:"pgo-import-col-checkbox",children:(0,i.jsx)(a.CheckboxControl,{checked:l,disabled:!r,onChange(){return t(e.row_number)},__nextHasNoMarginBottom:!0})}),(0,i.jsx)("td",{children:e.row_number}),(0,i.jsx)("td",{children:e.title||(0,n.__)("(no title)","puffergo")}),(0,i.jsx)("td",{children:(e.categories||[]).join(", ")||"—"}),(0,i.jsx)("td",{children:(_=e.price,c(_))}),(0,i.jsx)("td",{children:(x=e.moq,c(x))}),(0,i.jsx)("td",{children:0===p.length?"—":(0,i.jsxs)(i.Fragment,{children:[d.map(e=>`${e.key}: ${e.value}`).join("; "),u>0&&(0,i.jsx)("span",{className:"pgo-import-specs-more",children:(0,n.sprintf)(/* translators: %d: number of additional specs not shown */ /* translators: %d: number of additional specs not shown */
(0,n.__)(" +%d","puffergo"),u)})]})}),(0,i.jsx)("td",{children:(e.images||[]).length}),(0,i.jsxs)("td",{children:[(0,i.jsx)(f,{row:e}),m&&(0,i.jsx)(a.Button,{variant:"link",className:"pgo-import-toggle-errors",onClick(){return b(h?null:e.row_number)},children:h?(0,n.__)("Hide details","puffergo"):(0,n.__)("Show details","puffergo")})]})]}),m&&h&&(0,i.jsx)("tr",{className:"pgo-import-error-row",children:(0,i.jsx)("td",{colSpan:9,children:(0,i.jsx)("ul",{className:"pgo-import-error-list",children:e.errors.map((e,r)=>(0,i.jsx)("li",{children:e},r))})})})]},e.row_number);var x,_})})]})}),S&&(0,i.jsx)("div",{className:"pgo-import-overwrite-row",children:(0,i.jsx)(a.CheckboxControl,{label:(0,n.__)("Overwrite existing products with the same name","puffergo"),checked:p,onChange:d})}),(0,i.jsxs)("div",{className:"pgo-import-footer",children:[(0,i.jsx)(a.Button,{variant:"secondary",onClick:u,disabled:h,children:(0,n.__)("Back","puffergo")}),(0,i.jsx)(a.Button,{variant:"primary",onClick:m,disabled:0===k||h,isBusy:h,children:h?(0,n.__)("Importing…","puffergo"):(0,n.sprintf)(/* translators: %d: number of products selected for import */ /* translators: %d: number of products selected for import */
(0,n.__)("Import %d products as drafts","puffergo"),k)})]}),h&&(0,i.jsx)("p",{className:"pgo-import-progress-note",children:(0,n.__)("Importing products and downloading images — this can take a while. Please keep this page open.","puffergo")})]})},x=({config:e,result:r,onImportAnother:s,onUndo:t,undoing:l,undoDone:p,undoError:d})=>{var c,u,m,f;const[g,h]=(0,o.useState)(!1);if(!r)return null;const x=null!==(c=r.created?.length)&&void 0!==c?c:0,_=null!==(u=r.failed?.length)&&void 0!==u?u:0,j=null!==(m=r.image_failures?.length)&&void 0!==m?m:0,v=null!==(f=r.skipped)&&void 0!==f?f:0,w=e.productsUrl?e.productsUrl+"&post_status=draft":"#";return(0,i.jsxs)("div",{className:"pgo-import-result-step",children:[(0,i.jsxs)("div",{className:"pgo-import-result-cards",children:[(0,i.jsxs)("div",{className:"pgo-import-result-card is-created",children:[(0,i.jsx)("span",{className:"pgo-import-result-value",children:x}),(0,i.jsx)("span",{className:"pgo-import-result-label",children:(0,n.__)("Created","puffergo")})]}),(0,i.jsxs)("div",{className:"pgo-import-result-card",children:[(0,i.jsx)("span",{className:"pgo-import-result-value",children:v}),(0,i.jsx)("span",{className:"pgo-import-result-label",children:(0,n.__)("Skipped","puffergo")})]}),(0,i.jsxs)("div",{className:"pgo-import-result-card is-failed",children:[(0,i.jsx)("span",{className:"pgo-import-result-value",children:_}),(0,i.jsx)("span",{className:"pgo-import-result-label",children:(0,n.__)("Failed","puffergo")})]}),(0,i.jsxs)("div",{className:"pgo-import-result-card is-warning",children:[(0,i.jsx)("span",{className:"pgo-import-result-value",children:j}),(0,i.jsx)("span",{className:"pgo-import-result-label",children:(0,n.__)("Image failures","puffergo")})]})]}),_>0&&(0,i.jsxs)("div",{className:"pgo-import-result-details",children:[(0,i.jsx)("h3",{children:(0,n.__)("Failed rows","puffergo")}),(0,i.jsx)("ul",{children:r.failed.map((e,r)=>(0,i.jsx)("li",{children:(0,n.sprintf)(/* translators: 1: row number, 2: failure reason */ /* translators: 1: row number, 2: failure reason */
(0,n.__)("Row %1$s: %2$s","puffergo"),e.row_number,e.reason)},r))})]}),j>0&&(0,i.jsxs)("div",{className:"pgo-import-result-details",children:[(0,i.jsx)(a.Button,{variant:"link",onClick(){return h(e=>!e)},children:g?(0,n.__)("Hide image failures","puffergo"):(0,n.__)("Show image failures","puffergo")}),g&&(0,i.jsx)("ul",{className:"pgo-import-image-failure-list",children:r.image_failures.map((e,r)=>(0,i.jsx)("li",{children:(0,i.jsx)("a",{href:e,target:"_blank",rel:"noreferrer",children:e})},r))})]}),d&&(0,i.jsx)(a.Notice,{status:"error",children:d}),p&&(0,i.jsx)(a.Notice,{status:"success",children:(0,n.sprintf)(/* translators: %d: number of products deleted by the undo action */ /* translators: %d: number of products deleted by the undo action */
(0,n.__)("Undo complete — %d products deleted.","puffergo"),p)}),(0,i.jsxs)("div",{className:"pgo-import-footer",children:[(0,i.jsx)(a.Button,{variant:"primary",href:w,children:(0,n.__)("View imported drafts","puffergo")}),(0,i.jsx)(a.Button,{variant:"secondary",onClick:s,children:(0,n.__)("Import another file","puffergo")}),!p&&x>0&&(0,i.jsx)(a.Button,{variant:"link",onClick:t,isBusy:l,disabled:l,children:l?(0,n.__)("Undoing…","puffergo"):(0,n.__)("Undo this import","puffergo")}),l&&(0,i.jsx)(a.Spinner,{})]})]})},_=()=>window.puffergoProductImport||{},j=()=>{const e=(0,o.useMemo)(_,[]),[r,s]=(0,o.useState)(1),[t,p]=(0,o.useState)(!1),[c,u]=(0,o.useState)(null),[f,g]=(0,o.useState)([]),[j,v]=(0,o.useState)({}),[w,b]=(0,o.useState)(()=>new Set),[y,N]=(0,o.useState)(!1),[S,k]=(0,o.useState)(!1),[C,O]=(0,o.useState)(null),[B,D]=(0,o.useState)(null),[I,T]=(0,o.useState)(null),[U,P]=(0,o.useState)(!1),[F,E]=(0,o.useState)(null),[$,R]=(0,o.useState)(null),M=(0,o.useCallback)(()=>{s(1),p(!1),u(null),g([]),v({}),b(new Set),N(!1),k(!1),O(null),D(null),T(null),P(!1),E(null),R(null)},[]),z=(0,o.useCallback)(async r=>{p(!0),u(null);try{const o=new FormData;o.append("file",r);const t=await l()({url:e.restUrl+"/parse",method:"POST",body:o}),n=t.rows||[];g(n),v(t.summary||{});const a=new Set(n.filter(e=>!1!==e.valid&&!!e.title&&!e.duplicate).map(e=>e.row_number));b(a),N(!1),s(2)}catch(e){u(e?.message||(0,n.__)("Failed to parse the file.","puffergo"))}finally{p(!1)}},[e.restUrl]),A=(0,o.useCallback)(e=>{b(r=>{const s=new Set(r);return s.has(e)?s.delete(e):s.add(e),s})},[]),V=(0,o.useCallback)(e=>{b(e?new Set(f.filter(e=>!1!==e.valid&&!!e.title).map(e=>e.row_number)):new Set)},[f]),L=(0,o.useCallback)(async()=>{k(!0),O(null);try{var r;const o=f.filter(e=>w.has(e.row_number)),t=await l()({url:e.restUrl+"/import",method:"POST",data:{rows:o,overwrite_duplicates:y}});D(t),T(null!==(r=t?.batch_id)&&void 0!==r?r:null),R(null),E(null),s(3)}catch(e){O(e?.message||(0,n.__)("Failed to import products.","puffergo"))}finally{k(!1)}},[f,w,y,e.restUrl]),G=(0,o.useCallback)(async()=>{if(I&&window.confirm((0,n.__)("This will permanently delete the products created by this import. Continue?","puffergo"))){P(!0),E(null);try{var r;const s=await l()({url:e.restUrl+"/undo",method:"POST",data:{batch_id:I}});R(null!==(r=s?.deleted)&&void 0!==r?r:0)}catch(e){E(e?.message||(0,n.__)("Failed to undo the import.","puffergo"))}finally{P(!1)}}},[I,e.restUrl]);return(0,i.jsxs)(a.Card,{className:"pgo-product-import",children:[(0,i.jsxs)(a.CardHeader,{children:[(0,i.jsx)("h1",{className:"pgo-import-title",children:(0,n.__)("Import Products","puffergo")}),(0,i.jsx)(d,{currentStep:r})]}),(0,i.jsxs)(a.CardBody,{children:[C&&(0,i.jsx)(a.Notice,{status:"error",isDismissible:!0,onRemove(){return O(null)},children:C}),1===r&&(0,i.jsx)(m,{config:e,parsing:t,parseError:c,onFileSelected:z,onClearError(){return u(null)}}),2===r&&(0,i.jsx)(h,{rows:f,summary:j,selected:w,onToggleRow:A,onToggleAll:V,overwriteDuplicates:y,onOverwriteChange:N,onBack(){return s(1)},onImport:L,importing:S}),3===r&&(0,i.jsx)(x,{config:e,result:B,onImportAnother:M,onUndo:G,undoing:U,undoDone:$,undoError:F})]})]})};document.addEventListener("DOMContentLoaded",()=>{const e=document.getElementById("puffergo-product-import-root");if(!e)return;const r=window.puffergoProductImport||{};r.nonce&&l().use(l().createNonceMiddleware(r.nonce)),(0,o.createRoot)(e).render((0,i.jsx)(j,{}))})}},s={};function o(e){var t=s[e];if(void 0!==t)return t.exports;var l=s[e]={exports:{}};return r[e](l,l.exports,o),l.exports}o.m=r,e=[],o.O=(r,s,t,l)=>{if(!s){var n=1/0;for(d=0;d<e.length;d++){for(var[s,t,l]=e[d],a=!0,i=0;i<s.length;i++)(!1&l||n>=l)&&Object.keys(o.O).every(e=>o.O[e](s[i]))?s.splice(i--,1):(a=!1,n>l&&(n=l));if(a){e.splice(d--,1);var p=t();void 0!==p&&(r=p)}}return r}l=l||0;for(var d=e.length;d>0&&e[d-1][2]>l;d--)e[d]=e[d-1];e[d]=[s,t,l]},o.n=e=>{var r=e&&e.__esModule?()=>e.default:()=>e;return o.d(r,{a:r}),r},o.d=(e,r)=>{for(var s in r)o.o(r,s)&&!o.o(e,s)&&Object.defineProperty(e,s,{enumerable:!0,get:r[s]})},o.o=(e,r)=>({}.hasOwnProperty.call(e,r)),(()=>{var e={883:0,427:0};o.O.j=r=>0===e[r];var r=(r,s)=>{var t,l,[n,a,i]=s,p=0;if(n.some(r=>0!==e[r])){for(t in a)o.o(a,t)&&(o.m[t]=a[t]);if(i)var d=i(o)}for(r&&r(s);p<n.length;p++)l=n[p],o.o(e,l)&&e[l]&&e[l][0](),e[l]=0;return o.O(d)},s=globalThis.webpackChunkpuffergo_core=globalThis.webpackChunkpuffergo_core||[];s.forEach(r.bind(null,0)),s.push=r.bind(null,s.push.bind(s))})(),o.O(void 0,[427],()=>o(4778));var t=o.O(void 0,[427],()=>o(7122));t=o.O(t)})();