1import{u as ke,r as t,j as e}from"./vendor-react-GnwsTJfO.js";import{a4 as Ne,a2 as N,N as be,C as d,G as m,J as x,a1 as v,g as p,B as j,I as ye,n as q,aN as ve,bf as Ce,F as Se,f as h,V as a}from"./index-Bk3sb3g_.js";import{u as we}from"./useAdminAuth-B8BfgGDM.js";import{e as Ie,R as G}from"./AdminNavigation-DVMee0wC.js";import{C as Fe}from"./checkbox-CXoLB0wA.js";import{S as R}from"./switch-HlfwIZQ_.js";import{L as C}from"./label-CC4lcsSO.js";import{S}from"./scroll-area-Do1l-lmj.js";import{logActivity as Ke}from"./activityLog-D9S9TAPK.js";import{D as Te}from"./database-nLA1psU0.js";import{C as U}from"./circle-check-HbOHiPuy.js";import{C as X}from"./circle-x-DAuLvfD-.js";import{I as Ae}from"./image-CJtwgLmH.js";import{C as Re}from"./circle-alert-C33XTStY.js";import"./vendor-cjs-BcELrC9x.js";import"./vendor-supabase-BWPZKnGU.js";import"./vendor-ui-C1xCQL2z.js";import"./vendor-utils-B2rm_Apj.js";import"./vendor-query-Dp4tHR6-.js";import"./MasonicCross-CB_Vslf8.js";import"./users-Cq75-41X.js";/** 2 * @license lucide-react v0.462.0 - ISC 3 * 4 * This source code is licensed under the ISC license. 5 * See the LICENSE file in the root directory of this source tree. 6 */const W=Ne("FolderSync",[["path",{d:"M9 20H4a2 2 0 0 1-2-2V5a2 2 0 0 1 2-2h3.9a2 2 0 0 1 1.69.9l.81 1.2a2 2 0 0 0 1.67.9H20a2 2 0 0 1 2 2v.5",key:"1dkoa9"}],["path",{d:"M12 10v4h4",key:"1czhmt"}],["path",{d:"m12 14 1.535-1.605a5 5 0 0 1 8 1.5",key:"lvuxfi"}],["path",{d:"M22 22v-4h-4",key:"1ewp4q"}],["path",{d:"m22 18-1.535 1.605a5 5 0 0 1-8-1.5",key:"14ync0"}]]),es=()=>{ke();const{loading:Y}=we({requiredRoles:["webmaster"]}),[Z,ee]=t.useState(!1),[f,w]=t.useState(!1),[$,L]=t.useState(!1),[I,se]=t.useState([]),[c,F]=t.useState([]),[i,re]=t.useState(null),[g,te]=t.useState(!0),[B,ae]=t.useState(!0),[M,ie]=t.useState(!0),[b,ne]=t.useState(50),[K,_]=t.useState(!1),[D,E]=t.useState(0),[l,T]=t.useState(null),[le,ce]=t.useState(0),[oe,de]=t.useState(0),[H,z]=t.useState(!1),[y,O]=t.useState(null),[V,J]=t.useState(!1),[o,P]=t.useState(null),me=async()=>{L(!0);try{const{data:{session:s}}=await h.auth.getSession(),r=await h.functions.invoke("import-k2-articles",{body:{action:"test-connection"}});if(r.error)throw r.error;if(r.data.success)w(!0),a.success("Tilkobling til MySQL vellykket!"),await xe();else{w(!1),console.error("Connection failed:",r.data);const u=r.data.error||"Kunne ikke koble til MySQL",n=r.data.hint||"";a.error(u,{description:n,duration:1e4})}}catch(s){console.error("Connection test error:",s),w(!1),a.error("Feil ved tilkoblingstest: "+s.message,{description:"Sjekk at edge-funksjonen er deployet og at secrets er konfigurert.",duration:1e4})}finally{L(!1),ee(!0)}},xe=async()=>{try{const s=await h.functions.invoke("import-k2-articles",{body:{action:"get-categories"}});if(s.error)throw s.error;s.data.success&&se(s.data.categories||[])}catch(s){console.error("Error loading K2 categories:",s),a.error("Kunne ikke laste K2-kategorier")}},pe=async()=>{try{const s=await h.functions.invoke("import-k2-articles",{body:{action:"get-article-count",options:{categoryIds:c.length>0?c:void 0}}});if(s.error)throw s.error;s.data.success&&re(s.data.count)}catch(s){console.error("Error getting article count:",s)}};t.useEffect(()=>{f&&pe()},[c,f]);const Q=s=>{F(r=>r.includes(s)?r.filter(u=>u!==s):[...r,s])},he=()=>{F(I.map(s=>s.id))},ge=()=>{F([])},ue=async()=>{z(!0),O(null);try{const s=await h.functions.invoke("import-k2-articles",{body:{action:"sync-images"}});if(s.error)throw s.error;s.data.success?(O({synced:s.data.synced,skipped:s.data.skipped,total:s.data.total}),a.success(`${s.data.synced} bilder synkronisert til article_images`)):a.error("Synkronisering feilet: "+s.data.error)}catch(s){console.error("Sync error:",s),a.error("Feil ved synkronisering: "+s.message)}finally{z(!1)}},je=async()=>{J(!0),P(null);try{const s=await h.functions.invoke("import-k2-articles",{body:{action:"sync-categories"}});if(s.error)throw s.error;if(s.data.success){P({synced:s.data.synced,created:s.data.created||0,total:s.data.total,errors:s.data.errors});const r=[];s.data.synced>0&&r.push(`${s.data.synced} artikler oppdatert`),s.data.created>0&&r.push(`${s.data.created} nye kategorier opprettet`),r.length>0?a.success(r.join(", ")):a.info("Alle artikler har allerede kategori eller ingen K2-artikler funnet")}else a.error("Kategori-synkronisering feilet: "+s.data.error)}catch(s){console.error("Category sync error:",s),a.error("Feil ved kategori-synkronisering: "+s.message)}finally{J(!1)}},fe=async()=>{var u;if(i===null||i===0){a.error("Ingen artikler å importere");return}_(!0),T(null),E(0);const s=Math.ceil(i/b);de(s);const r={success:!0,imported:0,skipped:0,errors:[],totalAvailable:i,articles:[]};try{for(let n=0;n<s;n++){ce(n+1);const A=await h.functions.invoke("import-k2-articles",{body:{action:"import",options:{categoryIds:c.length>0?c:void 0,dryRun:g,skipExisting:B,importImages:M,limit:b,offset:n*b}}});if(A.error)throw A.error;
6const k=A.data;r.imported+=k.imported,r.skipped+=k.skipped,r.errors.push(...k.errors||[]),(u=r.articles)==null||u.push(...k.articles||[]),k.success||(r.success=!1),E((n+1)/s*100)}T(r),g?a.success(`Test fullført: ${r.imported} artikler ville bli importert`):(a.success(`Import fullført: ${r.imported} artikler importert`),r.imported>0&&await Ke(null,"k2_articles_imported","article",void 0,{title:`K2-import: ${r.imported} artikler`,details:`${r.imported} importert, ${r.skipped} hoppet over${r.errors.length?`, ${r.errors.length} feil`:""}`,imported:r.imported,skipped:r.skipped,errors:r.errors.length,categoryIds:c}))}catch(n){console.error("Import error:",n),a.error("Import feilet: "+n.message),r.success=!1,r.errors.push(n.message),T(r)}finally{_(!1)}};return Y?e.jsx("div",{className:"min-h-screen flex items-center justify-center",children:e.jsx(N,{className:"h-8 w-8 animate-spin"})}):e.jsxs("div",{className:"min-h-screen flex flex-col bg-background",children:[e.jsx(be,{}),e.jsx(Ie,{}),e.jsxs("main",{className:"flex-1 container mx-auto px-4 py-8",children:[e.jsxs("div",{className:"mb-8",children:[e.jsx("h1",{className:"text-3xl font-bold mb-2",children:"Importer fra Joomla K2"}),e.jsx("p",{className:"text-muted-foreground",children:"Importer artikler fra din eksisterende Joomla 3 / K2 installasjon"})]}),e.jsxs("div",{className:"grid gap-6 lg:grid-cols-2",children:[e.jsxs(d,{children:[e.jsxs(m,{children:[e.jsxs(x,{className:"flex items-center gap-2",children:[e.jsx(Te,{className:"h-5 w-5"}),"MySQL-tilkobling"]}),e.jsx(v,{children:"Test tilkoblingen til Joomla-databasen"})]}),e.jsx(p,{children:e.jsxs("div",{className:"flex items-center gap-4",children:[e.jsx(j,{onClick:me,disabled:$,children:$?e.jsxs(e.Fragment,{children:[e.jsx(N,{className:"h-4 w-4 mr-2 animate-spin"}),"Tester..."]}):e.jsxs(e.Fragment,{children:[e.jsx(G,{className:"h-4 w-4 mr-2"}),"Test tilkobling"]})}),Z&&e.jsx("div",{className:"flex items-center gap-2",children:f?e.jsxs(e.Fragment,{children:[e.jsx(U,{className:"h-5 w-5 text-green-500"}),e.jsx("span",{className:"text-green-600",children:"Tilkoblet"})]}):e.jsxs(e.Fragment,{children:[e.jsx(X,{className:"h-5 w-5 text-red-500"}),e.jsx("span",{className:"text-red-600",children:"Tilkobling feilet"})]})})]})})]}),e.jsxs(d,{children:[e.jsxs(m,{children:[e.jsxs(x,{className:"flex items-center gap-2",children:[e.jsx(Ae,{className:"h-5 w-5"}),"Synkroniser bilder"]}),e.jsx(v,{children:"Synkroniser eksisterende bilder fra articles.image_url til article_images-tabellen"})]}),e.jsxs(p,{className:"space-y-4",children:[e.jsx("p",{className:"text-sm text-muted-foreground",children:"Denne funksjonen finner artikler som har bilde-URL i articles-tabellen, men mangler oppføring i article_images-tabellen."}),e.jsx(j,{onClick:ue,disabled:H,children:H?e.jsxs(e.Fragment,{children:[e.jsx(N,{className:"h-4 w-4 mr-2 animate-spin"}),"Synkroniserer..."]}):e.jsxs(e.Fragment,{children:[e.jsx(G,{className:"h-4 w-4 mr-2"}),"Synkroniser bilder"]})}),y&&e.jsxs("div",{className:"grid grid-cols-3 gap-2 text-center text-sm",children:[e.jsxs("div",{className:"p-2 bg-green-50 dark:bg-green-950/20 rounded",children:[e.jsx("p",{className:"font-bold text-green-600",children:y.synced}),e.jsx("p",{className:"text-xs text-muted-foreground",children:"Synkronisert"})]}),e.jsxs("div",{className:"p-2 bg-amber-50 dark:bg-amber-950/20 rounded",children:[e.jsx("p",{className:"font-bold text-amber-600",children:y.skipped}),e.jsx("p",{className:"text-xs text-muted-foreground",children:"Hadde allerede"})]}),e.jsxs("div",{className:"p-2 bg-muted rounded",children:[e.jsx("p",{className:"font-bold",children:y.total}),e.jsx("p",{className:"text-xs text-muted-foreground",children:"Totalt sjekket"})]})]})]})]}),e.jsxs(d,{children:[e.jsxs(m,{children:[e.jsxs(x,{className:"flex items-center gap-2",children:[e.jsx(W,{className:"h-5 w-5"}),"Synkroniser kategorier"]}),e.jsx(v,{children:"Oppdater kategorier for importerte K2-artikler som mangler kategori"})]}),e.jsxs(p,{className:"space-y-4",children:[e.jsx("p",{className:"text-sm text-muted-foreground",children:"Denne funksjonen finner K2-artikler uten kategori og mapper dem basert på original_id i kategoriene. Krever at kategoriene er satt opp med riktig original_id."}),e.jsx(j,{onClick:je,disabled:V,children:V?e.jsxs(e.Fragment,{children:[e.jsx(N,{className:"h-4 w-4 mr-2 animate-spin"}),"Synkroniserer..."]}
6):e.jsxs(e.Fragment,{children:[e.jsx(W,{className:"h-4 w-4 mr-2"}),"Synkroniser kategorier"]})}),o&&e.jsxs("div",{className:"space-y-3",children:[e.jsxs("div",{className:"grid grid-cols-3 gap-2 text-center text-sm",children:[e.jsxs("div",{className:"p-2 bg-green-50 dark:bg-green-950/20 rounded",children:[e.jsx("p",{className:"font-bold text-green-600",children:o.synced}),e.jsx("p",{className:"text-xs text-muted-foreground",children:"Oppdatert"})]}),e.jsxs("div",{className:"p-2 bg-blue-50 dark:bg-blue-950/20 rounded",children:[e.jsx("p",{className:"font-bold text-blue-600",children:o.created}),e.jsx("p",{className:"text-xs text-muted-foreground",children:"Nye kategorier"})]}),e.jsxs("div",{className:"p-2 bg-muted rounded",children:[e.jsx("p",{className:"font-bold",children:o.total}),e.jsx("p",{className:"text-xs text-muted-foreground",children:"Totalt sjekket"})]})]}),o.errors&&o.errors.length>0&&e.jsxs("div",{className:"p-3 bg-amber-50 dark:bg-amber-950/20 rounded-md",children:[e.jsx("p",{className:"text-sm font-medium text-amber-800 dark:text-amber-200 mb-2",children:"Feil under synkronisering:"}),e.jsx(S,{className:"h-32",children:e.jsx("ul",{className:"text-xs text-amber-700 dark:text-amber-300 space-y-1",children:o.errors.map((s,r)=>e.jsxs("li",{children:["⢠",s]},r))})})]})]})]})]}),e.jsxs(d,{children:[e.jsx(m,{children:e.jsx(x,{children:"Importinnstillinger"})}),e.jsxs(p,{className:"space-y-4",children:[e.jsxs("div",{className:"flex items-center justify-between",children:[e.jsxs("div",{children:[e.jsx(C,{children:"Test-modus (dry run)"}),e.jsx("p",{className:"text-sm text-muted-foreground",children:"Ingen data lagres, kun simulering"})]}),e.jsx(R,{checked:g,onCheckedChange:te})]}),e.jsxs("div",{className:"flex items-center justify-between",children:[e.jsxs("div",{children:[e.jsx(C,{children:"Hopp over eksisterende"}),e.jsx("p",{className:"text-sm text-muted-foreground",children:"Ikke importer artikler som allerede finnes (basert på slug)"})]}),e.jsx(R,{checked:B,onCheckedChange:ae})]}),e.jsxs("div",{className:"flex items-center justify-between",children:[e.jsxs("div",{children:[e.jsx(C,{children:"Importer bilder"}),e.jsx("p",{className:"text-sm text-muted-foreground",children:"Lenk til hovedbilder fra K2"})]}),e.jsx(R,{checked:M,onCheckedChange:ie})]}),e.jsxs("div",{className:"space-y-2",children:[e.jsx(C,{children:"Batch-størrelse"}),e.jsx(ye,{type:"number",value:b,onChange:s=>ne(parseInt(s.target.value)||50),min:10,max:200}),e.jsx("p",{className:"text-sm text-muted-foreground",children:"Antall artikler per forespørsel (10-200)"})]})]})]}),f&&I.length>0&&e.jsxs(d,{className:"lg:col-span-2",children:[e.jsx(m,{children:e.jsxs("div",{className:"flex items-center justify-between",children:[e.jsxs("div",{children:[e.jsx(x,{children:"K2-kategorier"}),e.jsx(v,{children:"Velg hvilke kategorier som skal importeres"})]}),e.jsxs("div",{className:"flex gap-2",children:[e.jsx(j,{variant:"outline",size:"sm",onClick:he,children:"Velg alle"}),e.jsx(j,{variant:"outline",size:"sm",onClick:ge,children:"Fjern alle"})]})]})}),e.jsxs(p,{children:[e.jsx(S,{className:"h-60",children:e.jsx("div",{className:"grid grid-cols-2 md:grid-cols-3 lg:grid-cols-4 gap-3",children:I.map(s=>e.jsxs("div",{className:"flex items-center space-x-2 p-2 rounded border hover:bg-muted/50 cursor-pointer",onClick:()=>Q(s.id),children:[e.jsx(Fe,{checked:c.includes(s.id),onCheckedChange:()=>Q(s.id)}),e.jsx("span",{className:"text-sm",children:s.name}),e.jsx(q,{variant:"secondary",className:"ml-auto text-xs",children:s.id})]},s.id))})}),i!==null&&e.jsxs("div",{className:"mt-4 p-4 bg-muted rounded-lg",children:[e.jsx("p",{className:"font-medium",children:c.length===0?"Alle kategorier":`${c.length} kategorier valgt`}),e.jsxs("p",{className:"text-2xl font-bold",children:[i.toLocaleString("nb-NO")," artikler"]})]})]})]}),f&&e.jsxs(d,{className:"lg:col-span-2",children:[e.jsx(m,{children:e.jsxs(x,{className:"flex items-center gap-2",children:[e.jsx(ve,{className:"h-5 w-5"}),"Start import"]})}),e.jsxs(p,{className:"space-y-4",children:[K&&e.jsxs("div",{className:"space-y-2",children:[e.jsxs("div",{className:"flex justify-between text-sm",children:[e.jsxs("span",{children:["Batch ",le," av ",oe]}),e.jsxs("span",{children:[Math.round(D),"%"]})]}),e.jsx(Ce,{value:D})]}),e.jsx(j,{onClick:fe,disabled:K||i===null||i===0,className:"w-full",size:"lg",children:K?e.jsxs(e.Fragment,{children:[e.jsx(N,{className:"h-4 w-4 mr-2 animate-spin"}),"Importerer..."]}
6):g?"Kjør test (dry run)":`Importer ${(i==null?void 0:i.toLocaleString("nb-NO"))||0} artikler`}),g&&e.jsxs("div",{className:"flex items-center gap-2 text-amber-600 bg-amber-50 dark:bg-amber-950/20 p-3 rounded",children:[e.jsx(Re,{className:"h-5 w-5"}),e.jsx("span",{className:"text-sm",children:"Test-modus er aktivert. Ingen data vil bli lagret."})]})]})]}),l&&e.jsxs(d,{className:"lg:col-span-2",children:[e.jsx(m,{children:e.jsxs(x,{className:"flex items-center gap-2",children:[l.success?e.jsx(U,{className:"h-5 w-5 text-green-500"}):e.jsx(X,{className:"h-5 w-5 text-red-500"}),"Importresultat"]})}),e.jsxs(p,{className:"space-y-4",children:[e.jsxs("div",{className:"grid grid-cols-3 gap-4",children:[e.jsxs("div",{className:"text-center p-4 bg-green-50 dark:bg-green-950/20 rounded",children:[e.jsx("p",{className:"text-2xl font-bold text-green-600",children:l.imported}),e.jsx("p",{className:"text-sm text-muted-foreground",children:g?"Ville importeres":"Importert"})]}),e.jsxs("div",{className:"text-center p-4 bg-amber-50 dark:bg-amber-950/20 rounded",children:[e.jsx("p",{className:"text-2xl font-bold text-amber-600",children:l.skipped}),e.jsx("p",{className:"text-sm text-muted-foreground",children:"Hoppet over"})]}),e.jsxs("div",{className:"text-center p-4 bg-red-50 dark:bg-red-950/20 rounded",children:[e.jsx("p",{className:"text-2xl font-bold text-red-600",children:l.errors.length}),e.jsx("p",{className:"text-sm text-muted-foreground",children:"Feil"})]})]}),l.errors.length>0&&e.jsxs("div",{className:"space-y-2",children:[e.jsx("h4",{className:"font-medium text-red-600",children:"Feilmeldinger:"}),e.jsx(S,{className:"h-40 border rounded p-3",children:l.errors.map((s,r)=>e.jsx("p",{className:"text-sm text-red-600 mb-1",children:s},r))})]}),l.articles&&l.articles.length>0&&e.jsxs("div",{className:"space-y-2",children:[e.jsx("h4",{className:"font-medium",children:"Artikkeldetaljer:"}),e.jsx(S,{className:"h-60 border rounded",children:e.jsxs("table",{className:"w-full text-sm",children:[e.jsx("thead",{className:"sticky top-0 bg-background border-b",children:e.jsxs("tr",{children:[e.jsx("th",{className:"text-left p-2",children:"Tittel"}),e.jsx("th",{className:"text-left p-2",children:"Status"})]})}),e.jsx("tbody",{children:l.articles.map((s,r)=>e.jsxs("tr",{className:"border-b",children:[e.jsx("td",{className:"p-2 truncate max-w-xs",children:s.title}),e.jsxs("td",{className:"p-2",children:[e.jsxs(q,{variant:s.status==="imported"||s.status==="would-import"?"default":s.status==="skipped"?"secondary":"destructive",children:[s.status==="imported"&&"Importert",s.status==="would-import"&&"Ville importeres",s.status==="skipped"&&"Hoppet over",s.status==="error"&&"Feil"]}),s.error&&e.jsx("span",{className:"ml-2 text-xs text-red-500",children:s.error})]})]},r))})]})})]})]})]})]})]}),e.jsx(Se,{})]})};export{es as default};
Line numbers count LF bytes from the start of the resource, as the search results do. Vendor segments are library code the classifier recognised; they are stored but not indexed. Bytes are shown as Latin1 characters, one per byte.