1"use strict";(self.webpackChunk_N_E=self.webpackChunk_N_E||[]).push([[823],{40823:function(o,n,t){t.d(n,{T:function(){return u},v:function(){return d}});var i=t(57437),e=t(2265),a=t(59010),l=t(16822);let c=(0,e.createContext)(void 0);function r(o){"undefined"!=typeof document&&(document.cookie="currentLocationId=".concat(o,"; path=/; max-age=").concat(2592e3,"; SameSite=Lax"))}function d(o){let{children:n}=o,[t,d]=(0,e.useState)(null),[u,s]=(0,e.useState)([]),[I,f]=(0,e.useState)(!0),[p,y]=(0,e.useState)(!1),m=(0,e.useRef)(!1),{user:h,locations:v,locationsAvailable:L,refreshMetafields:C,refreshPermissions:g,delegationContext:S,refreshUser:N,isLoading:w}=(0,a.a)();(0,e.useEffect)(()=>{if(t){var o,n;localStorage.setItem("projectLocationDetails",JSON.stringify({locationId:t.locationId,shopifyCompanyId:null!==(o=t.shopifyCompanyId)&&void 0!==o?o:null,shopifyCompanyLocationId:null!==(n=t.shopifyCompanyLocationId)&&void 0!==n?n:null}))}else localStorage.removeItem("projectLocationDetails")},[t]);let E=(0,e.useRef)(N);E.current=N;let P=(0,e.useRef)(!1);(0,e.useEffect)(()=>{let o=()=>{P.current=!0,d(null),E.current({fresh:!0})};return window.addEventListener(l.Nc,o),()=>window.removeEventListener(l.Nc,o)},[]),(0,e.useEffect)(()=>{if(!w){if(S){m.current=!0,y(!1);let o=S.assignments.filter(o=>null!==o.locationId).map(o=>{var n,t,i;return{locationId:o.locationId,displayName:null!==(n=o.locationDisplayName)&&void 0!==n?n:"",companyId:S.companyId,shopifyCompanyId:null!==(t=o.shopifyCompanyId)&&void 0!==t?t:null,shopifyCompanyLocationId:null!==(i=o.shopifyCompanyLocationId)&&void 0!==i?i:null,isPrimaryLocation:!1}});if(o.length>0)s(o),d(n=>{var t;if(n&&o.some(o=>o.locationId===n.locationId))return o.find(o=>o.locationId===n.locationId)||n;let i=null!==(t=S.currentLocationId)&&void 0!==t?t:S.defaultLocationId;return o.find(o=>o.locationId===i)||o[0]||null}),f(!1);else{let o=!1;return(async()=>{try{let n=P.current;P.current=!1;let t=await fetch("/api/admin/delegations/company/".concat(S.companyId,"/locations").concat(n?"?fresh=1":""),{credentials:"include"});if(o)return;if(t.ok){let n=((await t.json()).locations||[]).map(o=>{var n,t,i;return{locationId:o.locationId,displayName:o.displayName,companyId:S.companyId,shopifyCompanyId:null!==(n=o.shopifyCompanyId)&&void 0!==n?n:null,shopifyCompanyLocationId:null!==(t=o.shopifyCompanyLocationId)&&void 0!==t?t:null,isPrimaryLocation:null!==(i=o.isPrimary)&&void 0!==i&&i}});o||(s(n),d(o=>{var t;if(o&&n.some(n=>n.locationId===o.locationId))return n.find(n=>n.locationId===o.locationId)||o;let i=null!==(t=S.currentLocationId)&&void 0!==t?t:S.defaultLocationId;return n.find(o=>o.locationId===i)||n[0]||null}))}}catch(o){}finally{o||f(!1)}})(),()=>{o=!0}}return}if(v&&v.length>0){m.current=!0,y(!1),s(v);let o=localStorage.getItem("currentLocationId");if(o){let n=v.find(n=>n.locationId===parseInt(o));if(n){d(o=>(null==o?void 0:o.locationId)===n.locationId?o:n),f(!1),C(n.locationId),g(n.locationId),r(n.locationId);return}}let n=v.find(o=>o.isPrimaryLocation)||v[0];d(o=>(null==o?void 0:o.locationId)===n.locationId?o:n),localStorage.setItem("currentLocationId",n.locationId.toString()),r(n.locationId),f(!1);return}if(!L){if(m.current)return;y(!0),f(!1);return}m.current=!0,y(!1),s([]),d(null),f(!1)}},[v,L,S,w]);let k=(0,e.useCallback)(o=>{if(d(o),o){if(S){var n,t,i;fetch("/api/admin/delegations/context/location",{method:"PATCH",credentials:"include",headers:{"Content-Type":"application/json"},body:JSON.stringify({locationId:o.locationId,displayName:null!==(n=o.displayName)&&void 0!==n?n:null,shopifyCompanyId:null!==(t=o.shopifyCompanyId)&&void 0!==t?t:null,shopifyCompanyLocationId:null!==(i=o.shopifyCompanyLocationId)&&void 0!==i?i:null})}).then(()=>Promise.all([C(o.locationId),g(o.locationId)])).then(()=>E.current()).catch(()=>{})}else localStorage.setItem("currentLocationId",o.locationId.toString()),r(o.locationId),C(o.locationId),g(o.locationId)}else localStorage.removeItem("currentLocationId")},[C,g,S]);return(0,i.jsx)(c.Provider,{value:{currentLocation:t,setCurrentLocation:k,availableLocations:u,isLoading:I,locationsUnavailable:p},children:n})}function u(){let o=(0,e.useContext)(c);if(void 0===o)throw Error("useLocation must be used within a LocationProvider");return o}}}]);
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.