/**
 * Minified by jsDelivr using Terser v5.39.0.
 * Original file: /npm/structured-field-values@2.0.4/index.js
 *
 * Do NOT use SRI with dynamically generated files! More information: https://www.jsdelivr.com/using-sri-with-dynamic-files
 */
function err(e,...r){r=r.map((e=>Array.isArray(e)?JSON.stringify(e):e instanceof Map?"Map{}":e instanceof Set?"Set{}":"object"==typeof e?JSON.stringify(e):String(e)));return e.map(((e,t)=>[e,r.at(t)])).flat().join("")}export class Item{constructor(e,r=null){Array.isArray(e)&&(e=e.map((e=>e instanceof Item?e:new Item(e)))),this.value=e,this.params=r}}export function encodeItem(e){return serializeItem(e)}export function encodeList(e){return serializeList(e)}export function encodeDict(e){return serializeDict(e)}export function decodeItem(e){try{const{input_string:r,value:t}=parseItem(e.trim());if(""!==r)throw new Error(err`failed to parse "${r}" as Item`);return t}catch(r){throw new Error(err`failed to parse "${e}" as Item`,{cause:r})}}export function decodeList(e){try{const{input_string:r,value:t}=parseList(e.trim());if(""!==r)throw new Error(err`failed to parse "${r}" as List`);return t}catch(r){throw new Error(err`failed to parse "${e}" as List`,{cause:r})}}export function decodeDict(e){try{const{input_string:r,value:t}=parseDictionary(e.trim());if(""!==r)throw new Error(err`failed to parse "${r}" as Dict`);return t}catch(r){throw new Error(err`failed to parse "${e}" as Dict`,{cause:r})}}export function serializeList(e){if(!1===Array.isArray(e))throw new Error(err`failed to serialize "${e}" as List`);return e.map((e=>(e instanceof Item==!1&&(e=new Item(e)),Array.isArray(e.value)?serializeInnerList(e):serializeItem(e)))).join(", ")}export function serializeInnerList(e){return`(${e.value.map(serializeItem).join(" ")})${serializeParams(e.params)}`}export function serializeParams(e){return null===e?"":Object.entries(e).map((([e,r])=>!0===r?`;${serializeKey(e)}`:`;${serializeKey(e)}=${serializeBareItem(r)}`)).join("")}export function serializeKey(e){if(!1===/^[a-z\*][a-z0-9\-\_\.\*]*$/.test(e))throw new Error(err`failed to serialize "${e}" as Key`);return e}export function serializeDict(e){if("object"!=typeof e)throw new Error(err`failed to serialize "${e}" as Dict`);const r=e instanceof Map?e.entries():Object.entries(e);return Array.from(r).map((([e,r])=>{r instanceof Item==!1&&(r=new Item(r));let t=serializeKey(e);return!0===r.value?t+=serializeParams(r.params):(t+="=",Array.isArray(r.value)?t+=serializeInnerList(r):t+=serializeItem(r)),t})).join(", ")}export function serializeItem(e){return e instanceof Item?`${serializeBareItem(e.value)}${serializeParams(e.params)}`:serializeBareItem(e)}export function serializeBareItem(e){switch(typeof e){case"number":if(!Number.isFinite(e))throw new Error(err`failed to serialize "${e}" as Bare Item`);return Number.isInteger(e)?serializeInteger(e):serializeDecimal(e);case"string":return serializeString(e);case"symbol":return serializeToken(e);case"boolean":return serializeBoolean(e);case"object":if(e instanceof Date)return serializeDate(e);if(e instanceof Uint8Array)return serializeByteSequence(e);default:throw new Error(err`failed to serialize "${e}" as Bare Item`)}}export function serializeInteger(e){if(e<-999999999999999n||999999999999999n<e)throw new Error(err`failed to serialize "${e}" as Integer`);return e.toString()}export function serializeDecimal(e){const r=roundToEven(e,3);if(Math.floor(Math.abs(r)).toString().length>12)throw new Error(err`failed to serialize "${e}" as Decimal`);const t=r.toString();return t.includes(".")?t:`${t}.0`}function roundToEven(e,r){if(e<0)return-roundToEven(-e,r);const t=Math.pow(10,r);if(Math.abs(e*t%1-.5)<Number.EPSILON){const r=Math.floor(e*t);return(r%2==0?r:r+1)/t}return Math.round(e*t)/t}export function serializeString(e){if(/[\x00-\x1f\x7f]+/.test(e))throw new Error(err`failed to serialize "${e}" as string`);return`"${e.replace(/\\/g,"\\\\").replace(/"/g,'\\"')}"`}export function serializeToken(e){const r=Symbol.keyFor(e);if(!1===/^([a-zA-Z\*])([\!\#\$\%\&\'\*\+\-\.\^\_\`\|\~\w\:\/]*)$/.test(r))throw new Error(err`failed to serialize "${r}" as token`);return r}export function serializeByteSequence(e){if(!1===ArrayBuffer.isView(e))throw new Error(err`failed to serialize "${e}" as Byte Sequence`);return`:${base64encode(e)}:`}export function serializeBoolean(e){if("boolean"!=typeof e)throw new Error(err`failed to serialize "${e}" as boolean`);return e?"?1":"?0"}export function serializeDate(e){return`@${serializeInteger(e.getTime()/1e3)}`}export function parseList(e){const r=[];for(;e.length>0;){const t=parseItemOrInnerList(e);if(r.push(t.value),0===(e=t.input_string.trim()).length)return{input_string:e,value:r};if(","!==e[0])throw new Error(err`failed to parse "${e}" as List`);if(0===(e=e.substring(1).trim()).length||","===e[0])throw new Error(err`failed to parse "${e}" as List`)}return{value:r,input_string:e}}export function parseItemOrInnerList(e){return"("===e[0]?parseInnerList(e):parseItem(e)}export function parseInnerList(e){if("("!==e[0])throw new Error(err`failed to parse "${e}" as Inner List`);e=e.substring(1);const r=[];for(;e.length>0;){if(")"===(e=e.trim())[0]){const t=parseParameters(e=e.substring(1));return{value:new Item(r,t.value),input_string:t.input_string}}const t=parseItem(e);if(r.push(t.value)," "!==(e=t.input_string)[0]&&")"!==e[0])throw new Error(err`failed to parse "${e}" as Inner List`)}throw new Error(err`failed to parse "${e}" as Inner List`)}export function parseDictionary(e,r={}){const t=[];function n(e){return!0===r?.use_map?new Map(e):Object.fromEntries(e)}for(;e.length>0;){let r;const i=parseKey(e),a=i.value;if("="===(e=i.input_string)[0]){const t=parseItemOrInnerList(e.substring(1));r=t.value,e=t.input_string}else{const t=parseParameters(e);r=new Item(!0,t.value),e=t.input_string}if(t.push([a,r]),0===(e=e.trim()).length)return{input_string:e,value:n(t)};if(","!==e[0])throw new Error(err`failed to parse "${e}" as Dict`);if(0===(e=e.substring(1).trim()).length||","===e[0])throw new Error(err`failed to parse "${e}" as Dict`)}return{value:n(t),input_string:e}}export function parseItem(e){const r=parseBareItem(e),t=r.value,n=parseParameters(e=r.input_string),i=n.value;e=n.input_string;return{value:new Item(t,i),input_string:e}}export function parseBareItem(e){const r=e[0];if('"'===r)return parseString(e);if(/^[\-0-9]/.test(r))return parseIntegerOrDecimal(e);if("?"===r)return parseBoolean(e);if(":"===r)return parseByteSequence(e);if(/^[a-zA-Z\*]/.test(r))return parseToken(e);if("@"===r)return parseDate(e);throw new Error(err`failed to parse "${e}" as Bare Item`)}export function parseParameters(e){let r=null;for(;e.length>0&&";"===e[0];){const t=parseKey(e=e.substring(1).trim()),n=t.value;let i=!0;if("="===(e=t.input_string)[0]){const r=parseBareItem(e=e.substring(1));i=r.value,e=r.input_string}null===r&&(r={}),r[n]=i}return{value:r,input_string:e}}export function parseKey(e){let r=0;if(!1===/^[a-z\*]$/.test(e[r]))throw new Error(err`failed to parse "${e}" as Key`);let t="";for(;e.length>r;){if(!1===/^[a-z0-9\_\-\.\*]$/.test(e[r]))return{value:t,input_string:e.substring(r)};t+=e[r],r++}return{value:t,input_string:e.substring(r)}}export function parseIntegerOrDecimal(e){const r=e;let t,n=1,i="";if("-"===e[0]&&(n=-1,e=e.substring(1)),e.length<=0)throw new Error(`failed to parse "${r}" as Integer or Decimal`);const a=/^(\d+)?/g,s=a.exec(e);if(0===s[0].length)throw new Error(`failed to parse "${r}" as Integer or Decimal`);if(i+=s[1],"."===(e=e.substring(a.lastIndex))[0]){if(i.length>12)throw new Error(`failed to parse "${r}" as Integer or Decimal`);const a=/^(\.\d+)?/g,s=a.exec(e);if(e=e.substring(a.lastIndex),0===s[0].length||s[1].length>4)throw new Error(`failed to parse "${r}" as Integer or Decimal`);if(i+=s[1],i.length>16)throw new Error(`failed to parse "${r}" as Integer or Decimal`);t=parseFloat(i)*n}else{if(i.length>15)throw new Error(`failed to parse "${r}" as Integer or Decimal`);if(t=parseInt(i)*n,t<-999999999999999n||999999999999999n<t)throw new Error(`failed to parse "${i}" as Integer or Decimal`)}return{value:t,input_string:e}}export function parseString(e){let r="",t=0;if('"'!==e[t])throw new Error(`failed to parse "${e}" as String`);for(t++;e.length>t;){if("\\"===e[t]){if(e.length<=t+1)throw new Error(`failed to parse "${e}" as String`);if(t++,'"'!==e[t]&&"\\"!==e[t])throw new Error(`failed to parse "${e}" as String`);r+=e[t]}else{if('"'===e[t])return{value:r,input_string:e.substring(++t)};if(/[\x00-\x1f\x7f]+/.test(e[t]))throw new Error(`failed to parse "${e}" as String`);r+=e[t]}t++}throw new Error(`failed to parse "${e}" as String`)}export function parseToken(e){if(!1===/^[a-zA-Z\*]$/.test(e[0]))throw new Error(`failed to parse "${e}" as Token`);const r=/^([\!\#\$\%\&\'\*\+\-\.\^\_\`\|\~\w\:\/]+)/g,t=r.exec(e)[1];return e=e.substring(r.lastIndex),{value:Symbol.for(t),input_string:e}}export function parseByteSequence(e){if(":"!==e[0])throw new Error(`failed to parse "${e}" as Byte Sequence`);if(!1===(e=e.substring(1)).includes(":"))throw new Error(`failed to parse "${e}" as Byte Sequence`);const r=/(^.*?)(:)/g,t=r.exec(e)[1];e=e.substring(r.lastIndex);return{value:base64decode(t),input_string:e}}export function parseBoolean(e){let r=0;if("?"!==e[r])throw new Error(`failed to parse "${e}" as Boolean`);if(r++,"1"===e[r])return{value:!0,input_string:e.substring(++r)};if("0"===e[r])return{value:!1,input_string:e.substring(++r)};throw new Error(`failed to parse "${e}" as Boolean`)}export function parseDate(e){let r=0;if("@"!==e[r])throw new Error(`failed to parse "${e}" as Date`);r++;const t=parseIntegerOrDecimal(e.substring(r));if(!1===Number.isInteger(t.value))throw new Error(`failed to parse "${e}" as Date`);return{value:new Date(1e3*t.value),input_string:t.input_string}}export function base64decode(e){return new Uint8Array([...atob(e)].map((e=>e.charCodeAt(0))))}export function base64encode(e){return btoa(String.fromCharCode(...e))}
//# sourceMappingURL=/sm/9ba8bd73c15115f3edfa39619d008d7de61ad010ed2f343a74fb53c9a0954047.map