PageSourceSearch

https://ot-saver.web.app/merchant/assets/BranchesManagement-DkJcZlKm.js

js ot-saver.web.app collected 2026-10-03 12:32:21 UTC 27,666 bytes, 1 lines download raw bytes

1import{r as h,j as e}from"./vendor-DjDfZ0fj.js";import{u as se,a as R,b as te,c as U}from"./index-CJTM2ysx.js";import{g as re,B as F,c as ae,u as G}from"./branchService-B1FkhYQx.js";import{C as ne}from"./ConfirmationModal-D1Ogp6ji.js";import{X as Q,n as le,P as ie,a as oe,T as de,I as ce}from"./lucide-icons-Crky37b1.js";import"./firebase-sdk-Cg5oTOPN.js";function me({open:l,branch:i,offers:r,onClose:d,onSave:c}){const[t,s]=h.useState([]);if(h.useEffect(()=>{!l||!i||s(i.assignedOfferIds)},[i,l]),!l||!i)return null;const x=f=>{s(b=>b.includes(f)?b.filter(j=>j!==f):[...b,f])};return e.jsx("div",{className:"fixed inset-0 z-50 flex items-center justify-center bg-slate-900/45 p-3 sm:p-6",children:e.jsxs("section",{className:"max-h-[92vh] w-full max-w-2xl overflow-y-auto rounded-xl border border-slateSoft-200 bg-white shadow-card",children:[e.jsxs("header",{className:"flex items-center justify-between border-b border-slateSoft-200 px-4 py-3 sm:px-6",children:[e.jsx("h3",{className:"text-lg font-semibold text-slateSoft-900",children:"Assign Offers to Branch"}),e.jsx("button",{type:"button",onClick:d,className:"rounded-md p-2 text-slateSoft-700 hover:bg-slateSoft-100","aria-label":"Close assign offers",children:e.jsx(Q,{className:"h-4 w-4"})})]}),e.jsxs("div",{className:"px-4 py-4 sm:px-6",children:[e.jsxs("p",{className:"mb-3 text-sm text-slateSoft-700",children:["Branch: ",e.jsx("span",{className:"break-words font-medium text-slateSoft-900",children:i.branchName})]}),e.jsxs("p",{className:"mb-2 text-xs text-slateSoft-600",children:["Selected offers: ",t.length]}),e.jsx("div",{className:"max-h-72 space-y-2 overflow-y-auto rounded-lg border border-slateSoft-200 p-3",children:r.map(f=>e.jsxs("label",{className:"flex items-start gap-2 rounded-md border border-slateSoft-100 px-3 py-2 text-sm",children:[e.jsx("input",{type:"checkbox",checked:t.includes(f.id),onChange:()=>x(f.id),className:"mt-0.5 h-4 w-4"}),e.jsxs("div",{className:"min-w-0",children:[e.jsx("p",{className:"break-words font-medium text-slateSoft-900",children:f.title}),e.jsxs("p",{className:"text-xs text-slateSoft-700",children:[f.id," - AED ",f.discountedPriceAed]})]})]},f.id))})]}),e.jsxs("footer",{className:"sticky bottom-0 flex justify-end gap-2 border-t border-slateSoft-200 bg-white px-4 py-3 sm:px-6",children:[e.jsx("button",{type:"button",onClick:d,className:"rounded-lg border border-slateSoft-200 px-4 py-2 text-sm font-medium text-slateSoft-700 hover:bg-slateSoft-100",children:"Cancel"}),e.jsx("button",{type:"button",onClick:()=>c(i.id,t),className:"rounded-lg bg-brand-600 px-4 py-2 text-sm font-medium text-white hover:bg-brand-700",children:"Save assigned offers"})]})]})})}function xe(l){const i=decodeURIComponent(l.trim());if(!i)return null;const r=[/!3d(-?\d+(?:\.\d+)?)!4d(-?\d+(?:\.\d+)?)/,/@(-?\d+(?:\.\d+)?),(-?\d+(?:\.\d+)?)/,/[?&](?:q|query|ll|destination)=(-?\d+(?:\.\d+)?),\s*(-?\d+(?:\.\d+)?)/];for(const d of r){const c=i.match(d);if(!c)continue;const t={latitude:Number(c[1]),longitude:Number(c[2])};if(K(t.latitude,t.longitude)===null)return t}return null}function K(l,i){return l===null||i===null||!Number.isFinite(l)||!Number.isFinite(i)?"Latitude and longitude are required so the branch shows on the map.":Math.abs(l)>90||Math.abs(i)>180?"Latitude must be between -90 and 90, longitude between -180 and 180.":l===0&&i===0?"Enter the branch's real coordinates (0, 0 is not a valid location).":null}const D={branchName:"",address:"",phoneNumber:"",openingHours:"",mapLocationPlaceholder:"",latitude:null,longitude:null,managerName:"",visibility:"visible",assignedOfferIds:[],performance:{redemptions:0,salesGeneratedAed:0,customerVisits:0,bestPerformingOffer:"-"}};function ue({open:l,mode:i,initialValues:r,onClose:d,onSubmit:c}){const[t,s]=h.useState(D),[x,f]=h.useState({});if(h.useEffect(()=>{if(l){if(i==="edit"&&r){s({branchName:r.branchName,address:r.address,phoneNumber:r.phoneNumber,openingHours:r.openingHours,mapLocationPlaceholder:r.mapLocationPlaceholder,latitude:r.latitude,longitude:r.longitude,managerName:r.managerName,visibility:r.visibility,assignedOfferIds:r.assignedOfferIds,performance:r.performance}),f({});return}s(D),f({})}},[r,i,l]),!l)return null;const b=(a,N)=>{s(C=>({...C,[a]:N}))},j=a=>{const N=xe(a);s(C=>({...C,mapLocationPlaceholder:a,...N??{}}))},o=(a,N)=>{b(a,N.trim()===""?null:Number(N))},y=()=>{const a={};t.branchName.trim()||(a.branchName="Branch name is required."),t.address.trim()||(a.address="Address is required."),t.phoneNumber.trim()||(a.phoneNumber="Phone number is required."),t.openingHours.trim()||(a.openingHours="Opening hours are required.");const N=K(t.latitude,t.longitude);return N&&(a.location=N),t.managerName.trim()||(a.managerName="Manager name is required."),f(a),Object.keys(a).length===0},P=()=>{y()&&c(t)};return e.jsx("div",{className:"fixed inset-0 z-50 flex items-center justify-center bg-slate-900/45 p-3 sm:p-6",children:e.jsxs("section",{className:"w-full max-w-3xl rounded-xl border border-slateSoft-200 bg-white shadow-card",children:[e.jsxs("header",{className:"flex items-center justify-between border-b border-slateSoft-200 px-4 py-3 sm:px-6",children:[e.jsx("h3",{className:"text-lg font-semibold text-slateSoft-900",children:i==="add"?"Add Branch":"Edit Branch"}),e.jsx("button",{type:"button",onClick:d,className:"rounded-md p-2 text-slateSoft-700 hover:bg-slateSoft-100","aria-label":"Close branch form",children:e.jsx(Q,{className:"h-4 w-4"})})]}),e.jsxs("div",{className:"grid gap-4 px-4 py-4 sm:grid-cols-2 sm:px-6",children:[e.jsxs("div",{children:[e.jsx("label",{className:"mb-1 block text-sm font-medium text-slateSoft-800",children:"Branch name"}),e.jsx("input",{value:t.branchName,onChange:a=>b("branchName",a.target.value),className:"w-full rounded-lg border border-slateSoft-200 px-3 py-2 text-sm outline-none ring-brand-500 focus:ring-1"}),x.branchName&&e.jsx("p",{className:"mt-1 text-xs text-rose-600",children:x.branchName})]}),e.jsxs("div",{children:[e.jsx("label",{className:"mb-1 block text-sm font-medium text-slateSoft-800",children:"Manager name"}),e.jsx("input",{value:t.managerName,onChange:a=>b("managerName",a.target.value),className:"w-full rounded-lg border border-slateSoft-200 px-3 py-2 text-sm outline-none ring-brand-500 focus:ring-1"}),x.managerName&&e.jsx("p",{className:"mt-1 text-xs text-rose-600",children:x.managerName})]}),e.jsxs("div",{className:"sm:col-span-2",children:[e.jsx("label",{className:"mb-1 block text-sm font-medium text-slateSoft-800",children:"Address"}),e.jsx("textarea",{value:t.address,onChange:a=>b("address",a.target.value),rows:2,className:"w-full rounded-lg border border-slateSoft-200 px-3 py-2 text-sm outline-none ring-brand-500 focus:ring-1"}),x.address&&e.jsx("p",{className:"mt-1 text-xs text-rose-600",children:x.address})]}),e.jsxs("div",{children:[e.jsx("label",{className:"mb-1 block text-sm font-medium text-slateSoft-800",children:"Phone number"}),e.jsx("input",{value:t.phoneNumber,onChange:a=>b("phoneNumber",a.target.value),className:"w-full rounded-lg border border-slateSoft-200 px-3 py-2 text-sm outline-none ring-brand-500 focus:ring-1"}),x.phoneNumber&&e.jsx("p",{className:"mt-1 text-xs text-rose-600",children:x.phoneNumber})]}),e.jsxs("div",{children:[e.jsx("label",{className:"mb-1 block text-sm font-medium text-slateSoft-800",children:"Opening hours"}),e.jsx("input",{value:t.openingHours,onChange:a=>b("openingHours",a.target.value),className:"w-full rounded-lg border border-slateSoft-200 px-3 py-2 text-sm outline-none ring-brand-500 focus:ring-1"}),x.openingHours&&e.jsx("p",{className:"mt-1 text-xs text-rose-600",children:x.openingHours})]}),e.jsxs("div",{className:"sm:col-span-2",children:[e.jsx("label",{className:"mb-1 block text-sm font-medium text-slateSoft-800",children:"Map location"}),e.jsxs("div",{className:"rounded-lg border border-dashed border-slateSoft-300 px-3 py-3",children:[e.jsxs("div",{className:"mb-2 inline-flex items-center gap-2 rounded-md bg-slateSoft-100 px-2 py-1 text-xs text-slateSoft-700",children:[e.jsx(le,{className:"h-3.5 w-3.5"}),"Required — this is where the branch pin appears on the OT Saver map"]}),e.jsx("input",{value:t.mapLocationPlaceholder,onChange:a=>j(a.target.value),placeholder:"Google Maps link (optional — fills in the coordinates)",className:"mb-1 w-full rounded-md border border-slateSoft-200 px-2 py-1.5 text-sm outline-none ring-brand-500 focus:ring-1"}),e.jsx("p",{className:"mb-3 text-xs text-slateSoft-500",children:"Short links (maps.app.goo.gl) don't include coordinates. Open the link, then copy the numbers from the browser address bar, or right-click the pin in Google Maps to copy them."}),e.jsxs("div",{className:"grid gap-3 sm:grid-cols-2",children:[e.jsxs("div",{children:[e.jsx("label",{className:"mb-1 block text-xs font-medium text-slateSoft-800",children:"Latitude"}),e.jsx("input",{type:"number",step:"any",value:t.latitude??"",onChange:a=>o("latitude",a.target.value),placeholder:"25.0609638",className:"w-full rounded-md border border-slateSoft-200 px-2 py-1.5 text-sm outline-none ring-brand-500 focus:ring-1"})]}),e.jsxs("div",{children:[e.jsx("label",{className:"mb-1 block text-xs font-medium text-slateSoft-800",children:"Longitude"}),e.jsx("input",{type:"number",step:"any",value:t.longitude??"",onChange:a=>o("longitude",a.target.value),placeholder:"55.20967",className:"w-full rounded-md border border-slateSoft-200 px-2 py-1.5 text-sm outline-none ring-brand-500 focus:ring-1"})]})]})]}),x.location&&e.jsx("p",{className:"mt-1 text-xs text-rose-600",children:x.location})]}),e.jsxs("div",{children:[e.jsx("label",{className:"mb-1 block text-sm font-medium text-slateSoft-800",children:"Visibility"}),e.jsxs("select",{value:t.visibility,onChange:a=>b("visibility",a.target.value),className:"w-full rounded-lg border border-slateSoft-200 px-3 py-2 text-sm outline-none ring-brand-500 focus:ring-1",children:[e.jsx("option",{value:"visible",children:"Visible"}),e.jsx("option",{value:"hidden",children:"Hidden"})]})]})]}),e.jsxs("footer",{className:"flex justify-end gap-2 border-t border-slateSoft-200 px-4 py-3 sm:px-6",children:[e.jsx("button",{type:"button",onClick:d,className:"rounded-lg border border-slateSoft-200 px-4 py-2 text-sm font-medium text-slateSoft-700 hover:bg-slateSoft-100",children:"Cancel"}),e.jsx("button",{type:"button",onClick:P,className:"rounded-lg bg-brand-600 px-4 py-2 text-sm font-medium text-white hover:bg-brand-700",children:i==="add"?"Add Branch":"Save Changes"})]})]})})}const fe={visible:"Visible",hidden:"Hidden"},he={visible:"bg-emerald-100 text-emerald-700",hidden:"bg-slate-200 text-slate-700"};function V({visibility:l}){return e.jsx("span",{className:`inline-flex rounded-full px-2.5 py-1 text-xs font-medium ${he[l]}`,children:fe[l]})}const Y=(l,i)=>{const r=new Map(i.map(d=>[d.id,d]));return l.assignedOfferIds.reduce((d,c)=>{const t=r.get(c);return t&&t.status==="active"?d+1:d},0)};function be({branches:l,offers:i,onEdit:r,onToggleVisibility:d,onAssignOffers:c,canManage:t=!0}){return e.jsxs(e.Fragment,{children:[e.jsx("div",{className:"hidden xl:block",children:e.jsx("div",{className:"-mx-1 overflow-x-auto px-1",children:e.jsxs("table",{className:"min-w-full text-left text-sm",children:[e.jsx("thead",{children:e.jsxs("tr",{className:"border-b border-slateSoft-200 text-xs uppercase tracking-wide text-slateSoft-700",children:[e.jsx("th",{className:"px-2 py-3",children:"Branch"}),e.jsx("th",{className:"px-2 py-3",children:"Manager"}),e.jsx("th",{className:"px-2 py-3",children:"Contact"}),e.jsx("th",{className:"px-2 py-3",children:"Hours"}),e.jsx("th",{className:"px-2 py-3",children:"Visibility"}),e.jsx("th",{className:"px-2 py-3",children:"Active Offers"}),e.jsx("th",{className:"px-2 py-3",children:"Performance"}),e.jsx("th",{className:"px-2 py-3",children:"Actions"})]})}),e.jsx("tbody",{children:l.map(s=>e.jsxs("tr",{className:`border-b border-slateSoft-100 last:border-none align-top ${s.visibility==="hidden"?"bg-slateSoft-50/70":""}`,children:[e.jsxs("td",{className:"max-w-[280px] px-2 py-3",children:[e.jsx("p",{className:"break-words font-medium text-slateSoft-900",children:s.branchName}),e.jsx("p",{className:"mt-1 break-words text-xs text-slateSoft-700",children:s.address}),e.jsx("p",{className:"text-xs text-slateSoft-600",children:s.id})]}),e.jsx("td",{className:"max-w-[170px] px-2 py-3 text-slateSoft-800",children:e.jsx("p",{className:"break-words",children:s.managerName})}),e.jsx("td",{className:"px-2 py-3 text-slateSoft-800",children:s.phoneNumber}),e.jsx("td",{className:"px-2 py-3 text-slateSoft-800",children:s.openingHours}),e.jsx("td",{className:"px-2 py-3",children:e.jsx(V,{visibility:s.visibility})}),e.jsx("td",{className:"px-2 py-3 font-medium text-slateSoft-900",children:Y(s,i)}),e.jsxs("td",{className:"px-2 py-3 text-slateSoft-800",children:[e.jsxs("p",{children:[s.performance.redemptions.toLocaleStr
1ing("en-US")," redemptions"]}),e.jsxs("p",{className:"text-xs text-brand-700",children:["AED ",s.performance.salesGeneratedAed.toLocaleString("en-US")," sales"]})]}),e.jsx("td",{className:"px-2 py-3",children:e.jsxs("div",{className:"flex flex-wrap gap-1.5",children:[e.jsx("button",{type:"button",onClick:()=>r(s),disabled:!t,className:"rounded-md border border-slateSoft-200 px-2 py-1 text-xs font-medium text-slateSoft-700 hover:bg-slateSoft-100",children:"Edit"}),e.jsx("button",{type:"button",onClick:()=>c(s),disabled:!t,className:"rounded-md border border-slateSoft-200 px-2 py-1 text-xs font-medium text-slateSoft-700 hover:bg-slateSoft-100",children:"Assign Offers"}),e.jsx("button",{type:"button",onClick:()=>d(s),disabled:!t,className:"rounded-md border border-slateSoft-200 px-2 py-1 text-xs font-medium text-slateSoft-700 hover:bg-slateSoft-100",children:s.visibility==="visible"?"Hide":"Show"})]})})]},s.id))})]})})}),e.jsx("div",{className:"grid gap-3 xl:hidden",children:l.map(s=>e.jsxs("article",{className:`rounded-lg border p-4 ${s.visibility==="hidden"?"border-slateSoft-300 bg-slateSoft-100":"border-slateSoft-200 bg-slateSoft-50"}`,children:[e.jsxs("div",{className:"flex items-start justify-between gap-2",children:[e.jsxs("div",{className:"min-w-0",children:[e.jsx("p",{className:"break-words text-sm font-semibold text-slateSoft-900",children:s.branchName}),e.jsx("p",{className:"break-words text-xs text-slateSoft-700",children:s.address})]}),e.jsx(V,{visibility:s.visibility})]}),s.visibility==="hidden"&&e.jsx("p",{className:"mt-2 rounded-md border border-slateSoft-300 bg-white px-2 py-1 text-xs text-slateSoft-700",children:"Hidden from customers in the OT Saver app, but still manageable here."}),e.jsxs("div",{className:"mt-3 grid grid-cols-2 gap-2 text-xs text-slateSoft-700",children:[e.jsxs("div",{children:[e.jsx("p",{children:"Manager"}),e.jsx("p",{className:"break-words font-medium text-slateSoft-900",children:s.managerName})]}),e.jsxs("div",{children:[e.jsx("p",{children:"Phone"}),e.jsx("p",{className:"font-medium text-slateSoft-900",children:s.phoneNumber})]}),e.jsxs("div",{children:[e.jsx("p",{children:"Active offers"}),e.jsx("p",{className:"font-medium text-slateSoft-900",children:Y(s,i)})]}),e.jsxs("div",{children:[e.jsx("p",{children:"Sales"}),e.jsxs("p",{className:"font-medium text-brand-700",children:["AED ",s.performance.salesGeneratedAed.toLocaleString("en-US")]})]})]}),e.jsxs("p",{className:"mt-2 text-xs text-slateSoft-700",children:["Hours: ",s.openingHours]}),e.jsxs("p",{className:"text-xs text-slateSoft-700",children:["Map: ",s.mapLocationPlaceholder]}),e.jsxs("div",{className:"mt-3 flex flex-wrap gap-1.5",children:[e.jsx("button",{type:"button",onClick:()=>r(s),disabled:!t,className:"rounded-md border border-slateSoft-200 px-2 py-1 text-xs font-medium text-slateSoft-700 hover:bg-slateSoft-100",children:"Edit"}),e.jsx("button",{type:"button",onClick:()=>c(s),disabled:!t,className:"rounded-md border border-slateSoft-200 px-2 py-1 text-xs font-medium text-slateSoft-700 hover:bg-slateSoft-100",children:"Assign Offers"}),e.jsx("button",{type:"button",onClick:()=>d(s),disabled:!t,className:"rounded-md border border-slateSoft-200 px-2 py-1 text-xs font-medium text-slateSoft-700 hover:bg-slateSoft-100",children:s.visibility==="visible"?"Hide":"Show"})]})]},s.id))})]})}const q=l=>l.toLocaleString("en-US");function pe({branches:l}){const i=l.reduce((d,c)=>(d.redemptions+=c.performance.redemptions,d.sales+=c.performance.salesGeneratedAed,d.visits+=c.performance.customerVisits,d),{redemptions:0,sales:0,visits:0}),r=l.length>0?[...l].sort((d,c)=>c.performance.salesGeneratedAed-d.performance.salesGeneratedAed)[0]:null;return e.jsxs("section",{className:"rounded-xl border border-slateSoft-200 bg-white p-4 shadow-card sm:p-5",children:[e.jsx("h3",{className:"text-lg font-semibold text-slateSoft-900",children:"Branch Performance Summary"}),e.jsx("p",{className:"mt-1 text-sm text-slateSoft-700",children:"Combined analytics across currently filtered branches."}),e.jsxs("div",{className:"mt-4 grid gap-3 sm:grid-cols-2 xl:grid-cols-4",children:[e.jsxs("div",{className:"rounded-lg border border-slateSoft-100 bg-slateSoft-50 p-3",children:[e.jsx("p",{className:"text-xs text-slateSoft-700",children:"Total Redemptions"}),e.jsx("p",{className:"mt-1 text-lg font-semibold text-slateSoft-900",children:q(i.redemptions)})]}),e.jsxs("div",{className:"rounded-lg border border-slateSoft-100 bg-slateSoft-50 p-3",children:[e.jsx("p",{className:"text-xs text-slateSoft-700",children:"Sales Generated"}),e.jsxs("p",{className:"mt-1 text-lg font-semibold text-brand-700",children:["AED ",q(i.sales)]})]}),e.jsxs("div",{className:"rounded-lg border border-slateSoft-100 bg-slateSoft-50 p-3",children:[e.jsx("p",{className:"text-xs text-slateSoft-700",children:"Customer Visits"}),e.jsx("p",{className:"mt-1 text-lg font-semibold text-slateSoft-900",children:q(i.visits)})]}),e.jsxs("div",{className:"rounded-lg border border-slateSoft-100 bg-slateSoft-50 p-3",children:[e.jsx("p",{className:"text-xs text-slateSoft-700",children:"Top Branch by Sales"}),e.jsx("p",{className:"mt-1 break-words text-sm font-semibold text-slateSoft-900",children:(r==null?void 0:r.branchName)??"-"}),e.jsxs("p",{className:"break-words text-xs text-slateSoft-700",children:["Best Offer: ",(r==null?void 0:r.performance.bestPerformingOffer)??"-"]})]})]})]})}function z(l){return`BR-${l.toString().padStart(3,"0")}`}function we({pendingAction:l,onConsumePendingAction:i}={}){const{merchantId:r,permissions:d}=se(),c=d.includes("manage-branches"),t=!r,[s,x]=h.useState(R),[f,b]=h.useState(t?"ready":"loading"),[j,o]=h.useState(null),[y,P]=h.useState(""),[a,N]=h.useState("all"),[C,S]=h.useState(!1),[O,$]=h.useState("add"),[B,v]=h.useState(null),[T,M]=h.useState(!1),[X,L]=h.useState(null),[p,w]=h.useState(null),k=async()=>
1{if(b("loading"),o(null),t){window.setTimeout(()=>{if(a==="hidden"&&y.trim().toLowerCase().includes("deira")){b("error"),o({type:"error",message:"Branch directory sync is delayed for hidden Deira entries. Please retry shortly."});return}x(R),b("ready")},600);return}if(!r){b("error"),o({type:"error",message:"Merchant context is missing for branch access."});return}try{const n=await re(r);x(n),b("ready")}catch(n){b("error"),n instanceof F?o({type:"error",message:n.message}):o({type:"error",message:"Unable to load branches from Firestore."})}};h.useEffect(()=>{k()},[r,t]),h.useEffect(()=>{l==="add-branch"&&c&&($("add"),v(null),S(!0),i==null||i())},[l,c,i]);const A=h.useMemo(()=>{const n=y.trim().toLowerCase();return s.filter(m=>{const u=a==="all"?!0:m.visibility===a,g=n.length===0?!0:[m.branchName,m.address,m.managerName,m.phoneNumber].join(" ").toLowerCase().includes(n);return u&&g})},[s,y,a]),J=()=>{if(!c){o({type:"error",message:"You do not have permission to add branches."});return}$("add"),v(null),S(!0)},W=n=>{if(!c){o({type:"error",message:"You do not have permission to edit branches."});return}$("edit"),v(n),S(!0)},Z=async n=>{if(!c){o({type:"error",message:"You do not have permission to manage branches."});return}if(t){if(a==="hidden"&&y.trim().toLowerCase().includes("deira")&&o({type:"error",message:"Branch directory sync is delayed for hidden Deira entries. Please retry shortly."}),O==="add"){const m=s.reduce((g,E)=>{const H=Number(E.id.replace("BR-",""));return Number.isFinite(H)?Math.max(g,H):g},100),u={id:z(m+1),...n};x(g=>[u,...g]),o({type:"success",message:`Branch "${u.branchName}" added successfully.`})}else B&&(x(m=>m.map(u=>u.id===B.id?{...u,...n}:u)),o({type:"success",message:`Branch "${n.branchName}" updated successfully.`}));S(!1),v(null);return}if(!r){o({type:"error",message:"Merchant context is missing. Sign in again and retry."});return}try{const m=O==="add"?`Branch "${n.branchName}" added successfully.`:`Branch "${n.branchName}" updated successfully.`;if(O==="add"){const u=s.reduce((E,H)=>{const I=Number(H.id.replace("BR-",""));return Number.isFinite(I)?Math.max(E,I):E},100),g=z(u+1);await ae(r,g,n,d)}else B&&await G(r,B.id,n,d);await k(),o({type:"success",message:m})}catch(m){m instanceof F?o({type:"error",message:m.message}):o({type:"error",message:"Unable to save branch changes right now."})}S(!1),v(null)},_=async(n,m)=>{if(!c){o({type:"error",message:"You do not have permission to assign offers to branches."});return}if(t){x(u=>u.map(g=>g.id===n?{...g,assignedOfferIds:m}:g)),M(!1),L(null),o({type:"success",message:"Offers assigned to branch successfully."});return}if(!r){o({type:"error",message:"Merchant context is missing. Sign in again and retry."});return}try{await G(r,n,{assignedOfferIds:m},d),await k(),M(!1),L(null),o({type:"success",message:"Offers assigned to branch successfully."})}catch(u){u instanceof F?o({type:"error",message:u.message}):o({type:"error",message:"Unable to assign offers right now."})}},ee=async()=>{if(!c){o({type:"error",message:"You do not have permission to update branch visibility."}),w(null);return}if(!p)return;const n=p,m=n.visibility==="visible"?"hidden":"visible";if(t){x(u=>u.map(g=>g.id===n.id?{...g,visibility:m}:g)),o({type:"success",message:`Branch "${n.branchName}" is now ${m}.`}),w(null);return}if(!r){o({type:"error",message:"Merchant context is missing. Sign in again and retry."}),w(null);return}try{await G(r,n.id,{visibility:m},d),await k(),o({type:"success",message:`Branch "${n.branchName}" is now ${m}.`})}catch(u){u instanceof F?o({type:"error",message:u.message}):o({type:"error",message:"Unable to update branch visibility right now."})}finally{w(null)}};return e.jsxs(e.Fragment,{children:[e.jsxs("div",{className:"mb-6 flex flex-col gap-4 xl:flex-row xl:items-center xl:justify-between",children:[e.jsxs("div",{children:[e.jsx("p",{className:"text-sm text-slateSoft-700",children:"Branch Management"}),e.jsx("h2",{className:"text-2xl font-semibold text-slateSoft-900",children:"Manage Branch Visibility and Performance"}),e.jsx("p",{className:"mt-1 text-xs text-slateSoft-600",children:"Keep branch listings updated, assign offers, and track branch-level analytics in AED."})]}),e.jsxs("button",{type:"button",onClick:J,disabled:!c,className:"inline-flex min-h-11 items-center gap-2 rounded-lg bg-brand-600 px-4 py-2 text-sm font-medium text-white hover:bg-brand-700",children:[e.jsx(ie,{className:"h-4 w-4"}),"Add Branch"]})]}),j&&e.jsxs("div",{className:`mb-4 flex items-start justify-between gap-3 rounded-lg border px-4 py-3 text-sm ${j.type==="success"?"border-emerald-200 bg-emerald-50 text-emerald-700":"border-rose-200 bg-rose-50 text-rose-700"}`,children:[e.jsx("p",{children:j.message}),e.jsx("button",{type:"button",onClick:()=>o(null),className:"font-medium underline",children:"Dismiss"})]}),t?e.jsx("div",{className:"mb-4 rounded-lg border border-slateSoft-200 bg-slateSoft-50 px-4 py-3 text-xs text-slateSoft-700",children:"Local/test fallback mode: using mock branch data."}):e.jsx("div",{className:"mb-4 rounded-lg border border-emerald-200 bg-emerald-50 px-4 py-3 text-xs text-emerald-800",children:"Live Firestore branch mode enabled. Assigned offers still use mock offer references in this step."}),e.jsxs("div",{className:"mb-4 grid gap-2 rounded-xl border border-slateSoft-200 bg-white p-4 sm:grid-cols-2 xl:grid-cols-4",children:[e.jsxs("div",{className:"xl:col-span-2",children:[e.jsx("label",{className:"mb-1 block text-xs font-medium uppercase tracking-wide text-slateSoft-700",children:"Search Branches"}),e.jsx("input",{value:y,onChange:n=>P(n.target.value),placeholder:"Search by branch name, manager, address, or phone",className:"min-h-11 w-full rounded-lg border border-slateSoft-200 px-3 py-2 text-sm outline-none ring-brand-500 focus:ring-1"})]}),e.jsxs("div",{children:[e.jsx("label",{className:"mb-1 block text-xs font-medium uppercase tracking-wide text-slateSoft-700",children:"Visibility"}),e.jsx("select",{value:a,onChange:n=>N(n.target.value),className:"min-h-11 w-full rounded-lg border border-slateSoft-200 px-3 py-2 text-sm outline-none ring-brand-500 focus:ring-1",children:te.map(n=>e.jsx("option",{value:n.value,children:n.label},n.value))})]}),e.jsx("div",{className:"flex items-end",children:e.jsx("button",{type:"button",onClick:k,className:"min-h-11 w-full rounded-lg border border-slateSoft-200 px-3 py-2 text-sm font-medium text-slateSoft-700 hover:bg-slateSoft-100",children:"Refresh Branches"})})]}),e.jsx(pe,{branches:A}),e.jsxs("section",{className:"mt-6 min-w-0 rounded-xl border border-slateSoft-200 bg-white p-4 shadow-card sm:p-5",children:[f==="loading"&&e.jsxs("div",{className:"flex min-h-64 flex-col items-center justify-center gap-2 rounded-lg border border-dashed border-slateSoft-200 bg-slateSoft-50",children:[e.jsx("div",{className:"rounded-full bg-white p-2 shadow-sm",children:e.jsx(oe,{className:"h-4 w-4 animate-spin text-slateSoft-700"})}),e.jsx("p",{className:"text-sm font-medium text-slateSoft-700",children:"Loading branches..."})]}),f==="error"&&e.jsxs("div",{className:"flex min-h-64 flex-col items-center justify-center gap-2 rounded-lg border border-rose-200 bg-rose-50 text-center",children:[e.jsx(de,{className:"h-5 w-5 text-rose-700"}),e.jsx("p",{className:"text-sm font-medium text-rose-700",children:"Could not load branch directory."}),e.jsx("button",{type:"button",onClick:k,className:"rounded-lg border border-rose-200 bg-white px-3 py-2 text-sm font-medium text-rose-700 hover:bg-rose-50",children:"Retry loading"})]}),f==="ready"&&A.length===0&&e.jsxs("div",{className:"flex min-h-64 flex-col items-center justify-center gap-2 rounded-lg border border-dashed border-slateSoft-200 bg-slateSoft-50",children:[e.jsx("div",{className:"rounded-full bg-white p-2 shadow-sm",children:e.jsx(ce,{className:"h-5 w-5 text-slate-400"})}),e.jsx("p",{className:"text-sm font-medium text-slateSoft-900",children:"No branches match your filters."})]}),f==="ready"&&A.length>0&&e.jsx(be,{branches:A,offers:U,onEdit:W,onAssignOffers:n=>{L(n),M(!0)},onToggleVisibility:w,canManage:c})]}),e.jsx(ue,{open:C,mode:O,initialValues:B??void 0,onClose:()=>{S(!1),v(null)},onSubmit:Z}),e.jsx(me,{open:T,branch:X,offers:U,onClose:()=>{M(!1),L(null)},onSave:_}),e.jsx(ne,{open:!!p,title:(p==null?void 0:p.visibility)==="visible"?"Hide branch":"Show branch",description:(p==null?void 0:p.visibility)==="visible"?`Hide "${(p==null?void 0:p.branchName)??"this branch"}" from customer visibility? Customers will not see this branch or redeem branch-linked offers until it is shown again.`:`Make "${(p==null?void 0:p.branchName)??"this branch"}" visible to customers? This branch and its assigned offers will become available for customer discovery and redemption.`,confirmLabel:(p==null?void 0:p.visibility)==="visible"?"Hide Branch":"Show Branch",onCancel:()=>w(null),onConfirm:ee})]})}
1export{we as BranchesManagement};

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.