1import{fetchConfig}from"@theme/utilities";export const LINE_ITEM_PERSONALIZATION_CART_ATTRIBUTE_KEY="line_item_personalization",SKIP_PERSONALIZATION_PROPERTY_KEY="_skip_personalization",SKIP_PERSONALIZATION_ATTRIBUTE_KEY=SKIP_PERSONALIZATION_PROPERTY_KEY;export function normalizePersonalizationJsonString(raw){return typeof raw!="string"?"":raw.replace(/"/g,'"').replace(/"/g,'"').replace(/\\u0026quot;/g,'"').replace(/\\\//g,"/").trim()}export function parseLineItemPersonalizationMap(raw){const normalized=normalizePersonalizationJsonString(typeof raw=="string"?raw:"");if(!normalized)return{};try{const parsed=JSON.parse(normalized);return!parsed||typeof parsed!="object"||Array.isArray(parsed)?{}:parsed}catch{return{}}}export function serializeLineItemPersonalizationMap(map){return JSON.stringify(map||{})}export function getPersonalizationEntryRawFromAttributes(attributes,pid){if(!pid)return"";const entry=parseLineItemPersonalizationMap(attributes?.[LINE_ITEM_PERSONALIZATION_CART_ATTRIBUTE_KEY])[pid];return!entry||typeof entry!="object"?"":JSON.stringify(entry)}export function getPersonalizationEntryRawFromAttributesWithLegacy(attributes,pid){const fromMap=getPersonalizationEntryRawFromAttributes(attributes,pid);if(fromMap)return fromMap;if(!pid||!attributes)return"";const legacyKey=`_pers_${pid}`,legacyValue=attributes[legacyKey];return typeof legacyValue=="string"?legacyValue.trim():""}export function resolvePersonalizationTextFromEntryRaw(raw,preferredLetterType=""){const parsed=parsePersonalizationEntryPayload(raw),preferred=preferredLetterType.trim();if(preferred){const exactMatch=parsed[preferred];if(typeof exactMatch=="string"&&exactMatch.trim())return exactMatch.trim();const normalizedPreferred=preferred.toLowerCase();for(const[key,value]of Object.entries(parsed))if(key.trim().toLowerCase()===normalizedPreferred&&typeof value=="string"&&value.trim())return value.trim()}const staticKeys=new Set(["Design","Thread Color","Vinyl Color","Foil Color","_selected-id","_selected-color","_swatch-color","_thread-color"]);for(const[key,value]of Object.entries(parsed))if(!(typeof value!="string"||!value.trim())&&!(key.startsWith("_")||staticKeys.has(key)))return value.trim();if(typeof raw!="string"||!raw.trim())return"";try{const normalized=normalizePersonalizationJsonString(raw),entry=JSON.parse(normalized),attributes=Array.isArray(entry?.attributes)?entry.attributes:[];for(const attribute of attributes){if(attribute?.key!=="_selected-letter-type")continue;const letterValue=attribute.value&&typeof attribute.value=="object"?attribute.value:null,text=typeof letterValue?.value=="string"?letterValue.value.trim():"";if(text)return text}}catch{return""}return""}export function parsePersonalizationEntryPayload(raw){if(!raw)return{};try{const normalized=typeof raw=="string"?normalizePersonalizationJsonString(raw):raw,parsed=typeof normalized=="string"?JSON.parse(normalized):normalized,attributes=Array.isArray(parsed?.attributes)?parsed.attributes:[],out={};for(const entry of attributes){if(!entry||typeof entry!="object")continue;const key=typeof entry.key=="string"?entry.key:"";if(key){if(key==="_selected-letter-type"){const letterType=entry.value&&typeof entry.value=="object"?entry.value:null,label=typeof letterType?.label=="string"?letterType.label.trim():"",value=typeof letterType?.value=="string"?letterType.value:"";label&&(out[label]=value);continue}typeof entry.value=="string"&&(out[key]=entry.value)}}return out}catch{return{}}}export function withSkipPersonalizationAttribute(payload,skipped){const attributes=Array.isArray(payload?.attributes)?payload.attributes.filter(entry=>entry?.key!==SKIP_PERSONALIZATION_ATTRIBUTE_KEY):[];return{...payload&&typeof payload=="object"?payload:{},attributes:[{key:SKIP_PERSONALIZATION_ATTRIBUTE_KEY,value:skipped?"true":"false"},...attributes]}}export function getSkipPersonalizationValueFromEntryRaw(raw){const value=parsePersonalizationEntryPayload(raw)[SKIP_PERSONALIZATION_ATTRIBUTE_KEY];return value==="true"||value==="false"?value:""}export function cartAttributePayloadHasPersonalizationAttributes(raw){const normalized=normalizePersonalizationJsonString(typeof raw=="string"?raw:"");if(!normalized)return!1;try{const parsed=JSON.parse(normalized),attributes=Array.isArray(parsed?.attributes)?parsed.attributes:[],skipValue=attributes.find(entry=>entry?.key===SKIP_PERSONALIZATION_ATTRIBUTE_KEY)?.value;return String(skipValue??"").trim()==="true"?!1:attributes.some(entry=>{if(!entry||typeof entry!="object")return!1;const key=typeof entry.key=="string"?entry.key.trim():"";if(!key||key===SKIP_PERSONALIZATION_ATTRIBUTE_KEY)return!1;if(key==="_selected-letter-type"){const letterType=entry.value&&typeof entry.value=="object"?entry.value:null,label=typeof letterType?.label=="string"?letterType.label.trim():"",value=typeof letterType?.value=="string"?letterType.value.trim():"";return!!(label&&value)}return typeof entry.value=="string"&&entry.value.trim()!==""})}catch{return!1}}export function setLineItemPersonalizationEntry(existingMapRaw,pid,entryPayload){const map=parseLineItemPersonalizationMap(existingMapRaw||"{}");return entryPayload==null?delete map[pid]:map[pid]=entryPayload,serializeLineItemPersonalizationMap(map)}export async function fetchCartAttributes(){try{const response=await fetch("/cart.js");if(!response.ok)return{};const cart=await response.json();return cart?.attributes&&typeof cart.attributes=="object"?cart.attributes:{}}catch{return{}}}export function patchLineItemPersonalizationMap(existingMapRaw,removePids=[],setEntries={}){const map=parseLineItemPersonalizationMap(existingMapRaw||"{}");for(const pid of removePids){const normalizedPid=typeof pid=="string"?pid.trim():"";normalizedPid&&delete map[normalizedPid]}for(const[pid,payload]of Object.entries(setEntries)){const normalizedPid=typeof pid=="string"?pid.trim():"";normalizedPid&&(payload==null?delete map[normalizedPid]:map[normalizedPid]=payload)}return serializeLineItemPersonalizationMap(map)}export async function patchLineItemPersonalizationMapOnCart({removePids=[],setEntries={},existingAttributes,sectionIds=[],sectionsUrl=window.location.pathname}){const normalizedRemovePids=removePids.map(pid=>
1typeof pid=="string"?pid.trim():"").filter(Boolean),normalizedSetEntries=Object.fromEntries(Object.entries(setEntries).filter(([pid])=>typeof pid=="string"&&pid.trim()));if(normalizedRemovePids.length===0&&Object.keys(normalizedSetEntries).length===0)return null;const attributes=existingAttributes??await fetchCartAttributes(),nextMapValue=patchLineItemPersonalizationMap(attributes[LINE_ITEM_PERSONALIZATION_CART_ATTRIBUTE_KEY],normalizedRemovePids,normalizedSetEntries),updatePayload={attributes:{[LINE_ITEM_PERSONALIZATION_CART_ATTRIBUTE_KEY]:nextMapValue}};sectionIds.length>0&&(updatePayload.sections=sectionIds.join(","),updatePayload.sections_url=sectionsUrl);try{const response=await fetch(Theme.routes.cart_update_url||"/cart/update.js",fetchConfig("json",{body:JSON.stringify(updatePayload)}));return response.ok?response.json():(console.warn("Personalization cart attribute patch failed:",response.status),null)}catch(error){return console.warn("Personalization cart attribute patch failed:",error),null}}export async function updateLineItemPersonalizationEntry({pid,payload,existingAttributes,sectionIds=[],sectionsUrl=window.location.pathname}){if(!pid)return null;let response;if(payload==null?response=await patchLineItemPersonalizationMapOnCart({removePids:[pid],existingAttributes,sectionIds,sectionsUrl}):response=await patchLineItemPersonalizationMapOnCart({setEntries:{[pid]:payload},existingAttributes,sectionIds,sectionsUrl}),!response)throw new Error("Failed to save personalization cart data");return response}export function getLineItemPidFromProperties(properties){if(!properties)return"";if(Array.isArray(properties)){const match=properties.find(entry=>Array.isArray(entry)&&entry[0]==="_pid");return match?.[1]!=null?String(match[1]).trim():""}if(typeof properties=="object"){const value=properties._pid;return typeof value=="string"?value.trim():value!=null?String(value).trim():""}return""}export function collectActivePersonalizationPidsFromCart(cart){const activePids=new Set;for(const item of cart?.items||[]){const pid=getLineItemPidFromProperties(item.properties);pid&&activePids.add(pid)}return activePids}export function getOrphanedPersonalizationPids(existingMapRaw,activePids){const map=parseLineItemPersonalizationMap(existingMapRaw||"{}");return Object.keys(map).filter(pid=>!activePids.has(pid))}export async function pruneOrphanedLineItemPersonalizationFromCart({cart,existingAttributes,sectionIds=[],sectionsUrl=window.location.pathname}){const attributes=existingAttributes??cart?.attributes??await fetchCartAttributes(),activePids=collectActivePersonalizationPidsFromCart(cart),orphanedPids=getOrphanedPersonalizationPids(attributes[LINE_ITEM_PERSONALIZATION_CART_ATTRIBUTE_KEY],activePids);return orphanedPids.length===0?null:patchLineItemPersonalizationMapOnCart({removePids:orphanedPids,existingAttributes:attributes,sectionIds,sectionsUrl})} 2//# sourceMappingURL=/cdn/shop/t/190/assets/line-item-personalization-cart.js.map?v=33007227031965528191788968624
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.