(function(n,a){typeof exports=="object"&&typeof module<"u"?a(exports):typeof define=="function"&&define.amd?define(["exports"],a):(n=typeof globalThis<"u"?globalThis:n||self,a(n.KthksgyProcedureCall={}))})(this,function(n){"use strict";var S=Object.defineProperty;var b=(n,a,l)=>a in n?S(n,a,{enumerable:!0,configurable:!0,writable:!0,value:l}):n[a]=l;var g=(n,a,l)=>(b(n,typeof a!="symbol"?a+"":a,l),l);n.PROCEDURE_CALL_ERROR_CODE=void 0,(e=>{e.INTERNAL="PROCEDURE_CALL_INTERNAL",e.TIMEOUT="PROCEDURE_CALL_TIMEOUT",e.UNDEFINED="PROCEDURE_CALL_UNDEFINED",e.UNINITIALIZED="PROCEDURE_CALL_UNINITIALIZED"})(n.PROCEDURE_CALL_ERROR_CODE||(n.PROCEDURE_CALL_ERROR_CODE={}));class a extends Error{constructor(o,t,r){super(t,r&&r.cause?{cause:r.cause}:void 0);g(this,"code");g(this,"data");g(this,"timestamp");this.code=o??"",this.name=a.name+"["+this.code+"]",this.timestamp=Date.now(),r&&r.data&&(this.data=r.data)}}const l="PROCEDURE_CALL",P="0.0.1",E="0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz";function m(e){const i=[];for(;e>0;e=Math.floor(e/E.length))i.unshift(E.at(e%E.length));return i.length>0?i.join(""):"0"}function D(e){return Array.from({length:Math.max(e,0)}).map(function(){return E[Math.floor(E.length*Math.random())]}).join("")}function A(e){return"`"+e.replaceAll("\\","\\\\").replaceAll("`","\\`")+"`"}const _=m(Date.now())+":"+D(4),R="pc::",d="pc",f=new Map,u=new Map;let C=0;const c=new Map;async function L(e,i,o,t){const r=_+":"+String(C++),s={data:i,index:0,key:r,name:e,p:d,timestamp:Date.now(),t:"req",v:0},w=new Promise(function(M,O){f.set(r,[M,O]),(t==null?void 0:t.timeout)!==void 0&&Number.isFinite(t.timeout)&&t.timeout>0&&setTimeout(function(){O(new a(n.PROCEDURE_CALL_ERROR_CODE.TIMEOUT)),f.delete(r)},t.timeout)});return await Promise.resolve(o(R+JSON.stringify(s))),w}async function h(e,i,o,t){if(typeof e=="object"&&e!==null&&"postMessage"in e&&typeof e.postMessage=="function")return L(i,o,e.postMessage.bind(e),t);throw console.error(`[${l}] \`target.postMessage\`が初期化されていないため、手続き\`${i}\`のリクエストを送信できません。`),new a(n.PROCEDURE_CALL_ERROR_CODE.UNINITIALIZED)}async function y(e,i,o){const t=v(e);if(t!==void 0)switch(t.t){case"req":{const r=c.get(t.name);r?await Promise.resolve(r(t.data,{payload:t})).then(function(s){return{data:s,index:t.index+1,key:t.key,name:t.name,p:d,timestamp:Date.now(),t:"res",v:0}}).catch(function(s){return s instanceof a?(console.debug(s),{code:s.code||n.PROCEDURE_CALL_ERROR_CODE.INTERNAL,...s.data!==void 0&&{data:s.data},index:t.index+1,key:t.key,...s.message&&{message:s.message},name:t.name,p:d,timestamp:s.timestamp,t:"err",v:0}):(console.error(s),{code:n.PROCEDURE_CALL_ERROR_CODE.INTERNAL,index:t.index+1,key:t.key,name:t.name,p:d,timestamp:Date.now(),t:"err",v:0})}).then(function(s){return i(R+JSON.stringify(s),t)}):(console.error(`[${l}] 手続き\`${t.name}\`が登録されていません。`),await Promise.resolve(i(R+JSON.stringify({code:n.PROCEDURE_CALL_ERROR_CODE.UNDEFINED,index:t.index+1,key:t.key,name:t.name,p:d,timestamp:Date.now(),t:"err",v:0}),t)));break}case"res":{const r=f.get(t.key);r?(r[0](t.data),f.delete(t.key)):console.error(`[${l}] リクエスト\`${t.name}:${t.key}\`のコールバックが存在しないため、レスポンスを受信できません。`);break}case"err":{const r=f.get(t.key);r?(r[1](new a(t.code,t.message,{data:t.data})),f.delete(t.key)):console.error(`[${l}] リクエスト\`${t.name}:${t.key}\`のコールバックが存在しないため、エラーを受信できません。`);break}}else o?await Promise.resolve(o(e)):console.error(`[${l}] ペイロード文字列\`${e}\`が無効な形式です。`)}function N(e,i){return e!==void 0?i!==void 0?u.get(e)===i:u.has(e):i!==void 0?Array.from(u.values()).includes(i):!1}function I(e,i){return e!==void 0?i!==void 0?c.get(e)===i:c.has(e):i!==void 0?Array.from(c.values()).includes(i):!1}function v(e){if(typeof e=="string"&&e.startsWith(R))try{const i=JSON.parse(e.slice(R.length));return typeof i=="object"&&i!==null&&i.p===d&&i.v===0?i:void 0}catch{return}else return}function T(e,i){return(!c.has(e)||c.get(e)===u.get(e))&&c.set(e,i),u.set(e,i),function(){c.get(e)===i&&c.delete(e),u.get(e)===i&&u.delete(e)}}function U(e,i){return c.set(e,i),function(){if(c.get(e)===i){c.delete(e);const o=u.get(e);o!==void 0&&c.set(e,o)}}}function k(){f.clear(),u.clear(),C=0,c.clear()}n.CHARACTERS=E,n.NAME=l,n.PROCEDURE_CALL_PAYLOAD_STRING_PREFIX=R,n.PROCEDURE_CALL_PROTOCOL=d,n.ProcedureCallError=a,n.VERSION=P,n.call=L,n.callTarget=h,n.generateDuosexagesimalString=m,n.generateRandomString=D,n.generateTemplateLiteralString=A,n.handler=y,n.isDefaultProcedureRegistered=N,n.isProcedureRegistered=I,n.registerDefaultProcedure=T,n.registerProcedure=U,n.reset=k,Object.defineProperty(n,Symbol.toStringTag,{value:"Module"})}); //# sourceMappingURL=index.umd.cjs.map