1import{ai as E,s as d,r as v,u as C,D as L,b as R,c as P,d as T,a6 as I,e as V,L as M,h as W,B as N,P as B,i as z}from"./index-BZo64Z7H.js";import{j as a}from"./ui-vendor-B12AOeKi.js";import{r as j}from"./react-vendor-DZt7wVAJ.js";import{I as F,g as G}from"./registry-CwmLi24N.js";import{S as H,a as K,b as O,c as J,d as $}from"./select-B9Z9mxdV.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 te=E("Library",[["path",{d:"m16 6 4 14",key:"ji33uf"}],["path",{d:"M12 6v14",key:"1n7gus"}],["path",{d:"M8 8v12",key:"1gg7y9"}],["path",{d:"M4 4v16",key:"6qkkli"}]]),q={algolia_feed:{table:"dealership_inventory_config",key:"id"},dealercom_feed:{table:"dealership_inventory_config",key:"id"},service_sheet:{table:"service_sheet_config",key:"id"},crm_push:{table:"crm_configuration",key:"code"}},Q=new Set(["id","dealership_id","dealership_code","created_at","updated_at","last_verified_at","last_sync_at","last_sync_status","last_sync_message","dealercom_last_sync_at","dealercom_last_raw_response","dealercom_cron_job_id","rows_matched","rows_unmatched","sync_status","discovery_status"]);function U(n){const i={};for(const[e,s]of Object.entries(n))Q.has(e)||(i[e]=s);return i}async function ie(n){const{orgId:i,integrationKey:e,name:s,description:p,actorId:l}=n,{data:c,error:_}=await d.from("organization_integrations").select("config").eq("organization_id",i).eq("integration_key",e).maybeSingle();if(_)throw _;const g=[],r=q[e];if(r){const{data:y}=await d.from("dealerships").select("id, code, name").eq("organization_id",i),w=(y??[]).map(o=>o.id),f=(y??[]).map(o=>o.code);
6if(w.length>0||f.length>0){let o=[];if(r.key==="id"){const{data:t}=await d.from(r.table).select("*").in("dealership_id",w);o=t??[]}else{const{data:t}=await d.from(r.table).select("*").in("dealership_code",f);o=t??[]}const x=o.filter(t=>e==="algolia_feed"?t.feed_type==="algolia":e==="dealercom_feed"?t.feed_type==="dealer_com"||t.feed_type==="dealer_inspire":!0);for(const t of x)g.push({match_hint:"primary",table:r.table,source_dealership_ref:r.key==="id"?t.dealership_id:t.dealership_code,row:U(t)})}}const m={version:1,integration_key:e,org_integration_config:(c==null?void 0:c.config)??null,dealership_configs:g},{data:u,error:h}=await d.from("integration_snapshots").insert({integration_key:e,name:s,description:p??null,source_organization_id:i,config_payload:m,created_by:l}).select("*").single();if(h)throw h;return await v({userId:l,module:"Admin",action:"Save Integration Snapshot",details:{snapshot_id:u.id,integration_key:e,source_organization_id:i,name:s,dealership_config_count:g.length}}),u}async function se(n){let i=d.from("integration_snapshots").select("id, integration_key, name, description, source_organization_id, config_payload, created_by, created_at, updated_at, organizations:source_organization_id(name)").order("created_at",{ascending:!1});n&&(i=i.eq("integration_key",n));const{data:e,error:s}=await i;if(s)throw s;return(e??[]).map(p=>{var l;return{...p,source_organization_name:((l=p.organizations)==null?void 0:l.name)??null}})}async function A(n){var u;const{snapshotId:i,targetOrgId:e,actorId:s}=n,{data:p,error:l}=await d.from("integration_snapshots").select("*").eq("id",i).single();if(l)throw l;const c=p.config_payload,_=p.integration_key,{data:g}=await d.from("organization_integrations").select("id").eq("organization_id",e).eq("integration_key",_).maybeSingle();if(g!=null&&g.id){const{error:h}=await d.from("organization_integrations").update({config:c.org_integration_config??{}}).eq("id",g.id);if(h)throw h}else{const{error:h}=await d.from("organization_integrations").insert({organization_id:e,integration_key:_,status:"active",config:c.org_integration_config??{}});if(h)throw h}let r=0;const m=q[_];if(m&&((u=c.dealership_configs)==null?void 0:u.length)>0){const{data:h}=await d.from("dealerships").select("id, code, name").eq("organization_id",e),y=h??[];if(y.length>0){const w=[];for(let f=0;f<y.length;f++){const o=c.dealership_configs[f]??c.dealership_configs[0];o&&w.push({target:y[f],entry:o})}for(const{target:f,entry:o}of w){const x={...o.row},t=m.key==="id"?{...x,dealership_id:f.id}:{...x,dealership_code:f.code},k=m.key==="id"?"dealership_id":"dealership_code",D=m.key==="id"?f.id:f.code,{data:S}=await d.from(o.table).select("id").eq(k,D).maybeSingle();if(S!=null&&S.id){const{error:b}=await d.from(o.table).update(t).eq("id",S.id);if(b)throw b}else{const{error:b}=await d.from(o.table).insert(t);if(b)throw b}r++}}}return await v({userId:s,module:"Admin",action:"Apply Integration Snapshot",details:{snapshot_id:i,snapshot_name:p.name,integration_key:_,source_organization_id:p.source_organization_id,target_organization_id:e,dealership_rows_written:r}}),{orgConfigWritten:!0,dealershipRowsWritten:r}}async function re(n,i){const{data:e}=await d.from("integration_snapshots").select("name, integration_key").eq("id",n).single(),{error:s}=await d.from("integration_snapshots").delete().eq("id",n);if(s)throw s;await v({userId:i,module:"Admin",action:"Delete Integration Snapshot",details:{snapshot_id:n,snapshot_name:e==null?void 0:e.name,integration_key:e==null?void 0:e.integration_key}})}async function ne(n){const{sourceOrgId:i,targetOrgId:e,actorId:s}=n,{data:p,error:l}=await d.from("integration_snapshots").select("id, integration_key, created_at").eq("source_organization_id",i).order("created_at",{ascending:!1});if(l)throw l;const c=new Map;for(const r of p??[])c.has(r.integration_key)||c.set(r.integration_key,r);let _=0,g=0;for(const r of F){const m=c.get(r.key);if(!m){g++;continue}try{await A({snapshotId:m.id,targetOrgId:e,actorId:s}),_++}catch(u){console.warn("Apply snapshot failed for",r.key,u),g++}}return{applied:_,skipped:g}}function oe({open:n,onOpenChange:i,snapshot:e,targetOrgId:s,targetOrgName:p,onApplied:l}){var o,x;const{user:c}=C(),[_,g]=j.useState(!1),[r,m]=j.useState([]),[u,h]=j.useState(s??"");if(j.useEffect(()=>{h(s??"")},[s,e==null?void 0:e.id]),j.useEffect(()=>{!n||s||d.from("organizations").select("id, name").order("name").then(({data:t})=>m(t??[]))},[n,s]),!e)return null;const y=G(e.integration_key),w=((x=(o=e.config_payload)==null?void 0:o.dealership_configs)==null?void 0:x.length)??0;async function f(){if(!c)return;const t=u||s;if(!t){z.error("Pick a target organization");return}g(!0);try{const k=await A({snapshotId:e.id,targetOrgId:t,actorId:c.id});z.success(`Applied â ${k.dealershipRowsWritten} dealership row(s) written`),i(!1),l==null||l()}catch(k){z.error((k==null?void 0:k.message)??"Apply failed")}finally{g(!1)}}return a.jsx(L,{open:n,onOpenChange:i,children:a.jsxs(R,{className:"",children:[a.jsxs(P,{children:[a.jsxs(T,{className:"flex items-center gap-2",children:[a.jsx(I,{className:"h-5 w-5"}),"Apply Snapshot"]}),a.jsxs(V,{children:["Writes ",a.jsx("strong",{children:e.name})," (",(y==null?void 0:y.label)??e.integration_key,") onto the target organization. Existing config is overwritten."]})]}),a.jsxs("div",{className:"space-y-4 py-2",children:[s?a.jsxs("div",{className:"text-sm",children:[a.jsx("span",{className:"text-muted-foreground",children:"Target organization: "}),a.jsx("span",{className:"font-semibold",children:p})]}):a.jsxs("div",{className:"space-y-2",children:[a.jsx(M,{children:"Target organization"}),a.jsxs(H,{value:u,onValueChange:h,children:[a.jsx(K,{children:a.jsx(O,{placeholder:"Pick an organizationâ¦"})}),a.jsx(J,{children:r.map(t=>a.jsx($,{value:t.id,children:t.name},t.id))})]})]}),a.jsxs("div",{className:"rounded border bg-muted/40 p-3 text-xs space-y-1",children:[a.jsxs("div",{children:[a.jsx("span",{className:"text-muted-foreground",children:"Per-dealership rows captured: "}),a.jsx("span",{className:"font-semibold",children:w})]}),w>0&&a.jsx("p",{className:"text-muted-foreground",children:"Rows are paired
6in order with the target org's dealerships. If the target has fewer dealerships, extra snapshot rows are skipped. If it has more, all are seeded from the first snapshot row."})]}),a.jsxs("details",{className:"text-xs",children:[a.jsx("summary",{className:"cursor-pointer text-muted-foreground hover:text-foreground",children:"View payload"}),a.jsx("pre",{className:"mt-2 max-h-64 overflow-auto rounded bg-muted/60 p-2",children:JSON.stringify(e.config_payload,null,2)})]})]}),a.jsxs(W,{children:[a.jsx(N,{variant:"outline",onClick:()=>i(!1),disabled:_,children:"Cancel"}),a.jsxs(N,{onClick:f,disabled:_,children:[_?a.jsx(B,{className:"h-4 w-4 mr-2 animate-spin"}):a.jsx(I,{className:"h-4 w-4 mr-2"}),"Apply Snapshot"]})]})]})})}export{oe as A,te as L,ne as a,ie as c,re as d,se as l};
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.