// Smartpay Checkout SDK v0.1.0
!function(r,e){"object"==typeof exports&&"undefined"!=typeof module?module.exports=e():"function"==typeof define&&define.amd?define(e):(r="undefined"!=typeof globalThis?globalThis:r||self).Smartpay=e()}(this,(function(){"use strict";var r={},e=r=>encodeURIComponent(r).replace(/[!'()*]/g,(r=>`%${r.charCodeAt(0).toString(16).toUpperCase()}`)),t="%[a-f0-9]{2}",n=new RegExp(t,"gi"),o=new RegExp("("+t+")+","gi");function a(r,e){try{return decodeURIComponent(r.join(""))}catch(r){}if(1===r.length)return r;e=e||1;var t=r.slice(0,e),n=r.slice(e);return Array.prototype.concat.call([],a(t),a(n))}function i(r){try{return decodeURIComponent(r)}catch(o){for(var e=r.match(n),t=1;t<e.length;t++)e=(r=a(e,t).join("")).match(n);return r}}var c=function(r){if("string"!=typeof r)throw new TypeError("Expected `encodedURI` to be of type `string`, got `"+typeof r+"`");try{return r=r.replace(/\+/g," "),decodeURIComponent(r)}catch(e){return function(r){for(var e={"%FE%FF":"��","%FF%FE":"��"},t=o.exec(r);t;){try{e[t[0]]=decodeURIComponent(t[0])}catch(r){var n=i(t[0]);n!==t[0]&&(e[t[0]]=n)}t=o.exec(r)}e["%C2"]="�";for(var a=Object.keys(e),c=0;c<a.length;c++){var s=a[c];r=r.replace(new RegExp(s,"g"),e[s])}return r}(r)}},s=(r,e)=>{if("string"!=typeof r||"string"!=typeof e)throw new TypeError("Expected the arguments to be of type `string`");if(""===e)return[r];const t=r.indexOf(e);return-1===t?[r]:[r.slice(0,t),r.slice(t+e.length)]},u=function(r,e){for(var t={},n=Object.keys(r),o=Array.isArray(e),a=0;a<n.length;a++){var i=n[a],c=r[i];(o?-1!==e.indexOf(i):e(i,c,r))&&(t[i]=c)}return t};!function(r){const t=e,n=c,o=s,a=u,i=Symbol("encodeFragmentIdentifier");function l(r){if("string"!=typeof r||1!==r.length)throw new TypeError("arrayFormatSeparator must be single character string")}function p(r,e){return e.encode?e.strict?t(r):encodeURIComponent(r):r}function f(r,e){return e.decode?n(r):r}function d(r){return Array.isArray(r)?r.sort():"object"==typeof r?d(Object.keys(r)).sort(((r,e)=>Number(r)-Number(e))).map((e=>r[e])):r}function y(r){const e=r.indexOf("#");return-1!==e&&(r=r.slice(0,e)),r}function m(r){const e=(r=y(r)).indexOf("?");return-1===e?"":r.slice(e+1)}function g(r,e){return e.parseNumbers&&!Number.isNaN(Number(r))&&"string"==typeof r&&""!==r.trim()?r=Number(r):!e.parseBooleans||null===r||"true"!==r.toLowerCase()&&"false"!==r.toLowerCase()||(r="true"===r.toLowerCase()),r}function b(r,e){l((e=Object.assign({decode:!0,sort:!0,arrayFormat:"none",arrayFormatSeparator:",",parseNumbers:!1,parseBooleans:!1},e)).arrayFormatSeparator);const t=function(r){let e;switch(r.arrayFormat){case"index":return(r,t,n)=>{e=/\[(\d*)\]$/.exec(r),r=r.replace(/\[\d*\]$/,""),e?(void 0===n[r]&&(n[r]={}),n[r][e[1]]=t):n[r]=t};case"bracket":return(r,t,n)=>{e=/(\[\])$/.exec(r),r=r.replace(/\[\]$/,""),e?void 0!==n[r]?n[r]=[].concat(n[r],t):n[r]=[t]:n[r]=t};case"comma":case"separator":return(e,t,n)=>{const o="string"==typeof t&&t.includes(r.arrayFormatSeparator),a="string"==typeof t&&!o&&f(t,r).includes(r.arrayFormatSeparator);t=a?f(t,r):t;const i=o||a?t.split(r.arrayFormatSeparator).map((e=>f(e,r))):null===t?t:f(t,r);n[e]=i};case"bracket-separator":return(e,t,n)=>{const o=/(\[\])$/.test(e);if(e=e.replace(/\[\]$/,""),!o)return void(n[e]=t?f(t,r):t);const a=null===t?[]:t.split(r.arrayFormatSeparator).map((e=>f(e,r)));void 0!==n[e]?n[e]=[].concat(n[e],a):n[e]=a};default:return(r,e,t)=>{void 0!==t[r]?t[r]=[].concat(t[r],e):t[r]=e}}}(e),n=Object.create(null);if("string"!=typeof r)return n;if(!(r=r.trim().replace(/^[?#&]/,"")))return n;for(const a of r.split("&")){if(""===a)continue;let[r,i]=o(e.decode?a.replace(/\+/g," "):a,"=");i=void 0===i?null:["comma","separator","bracket-separator"].includes(e.arrayFormat)?i:f(i,e),t(f(r,e),i,n)}for(const r of Object.keys(n)){const t=n[r];if("object"==typeof t&&null!==t)for(const r of Object.keys(t))t[r]=g(t[r],e);else n[r]=g(t,e)}return!1===e.sort?n:(!0===e.sort?Object.keys(n).sort():Object.keys(n).sort(e.sort)).reduce(((r,e)=>{const t=n[e];return Boolean(t)&&"object"==typeof t&&!Array.isArray(t)?r[e]=d(t):r[e]=t,r}),Object.create(null))}r.extract=m,r.parse=b,r.stringify=(r,e)=>{if(!r)return"";l((e=Object.assign({encode:!0,strict:!0,arrayFormat:"none",arrayFormatSeparator:","},e)).arrayFormatSeparator);const t=t=>e.skipNull&&null==r[t]||e.skipEmptyString&&""===r[t],n=function(r){switch(r.arrayFormat){case"index":return e=>(t,n)=>{const o=t.length;return void 0===n||r.skipNull&&null===n||r.skipEmptyString&&""===n?t:null===n?[...t,[p(e,r),"[",o,"]"].join("")]:[...t,[p(e,r),"[",p(o,r),"]=",p(n,r)].join("")]};case"bracket":return e=>(t,n)=>void 0===n||r.skipNull&&null===n||r.skipEmptyString&&""===n?t:null===n?[...t,[p(e,r),"[]"].join("")]:[...t,[p(e,r),"[]=",p(n,r)].join("")];case"comma":case"separator":case"bracket-separator":{const e="bracket-separator"===r.arrayFormat?"[]=":"=";return t=>(n,o)=>void 0===o||r.skipNull&&null===o||r.skipEmptyString&&""===o?n:(o=null===o?"":o,0===n.length?[[p(t,r),e,p(o,r)].join("")]:[[n,p(o,r)].join(r.arrayFormatSeparator)])}default:return e=>(t,n)=>void 0===n||r.skipNull&&null===n||r.skipEmptyString&&""===n?t:null===n?[...t,p(e,r)]:[...t,[p(e,r),"=",p(n,r)].join("")]}}(e),o={};for(const e of Object.keys(r))t(e)||(o[e]=r[e]);const a=Object.keys(o);return!1!==e.sort&&a.sort(e.sort),a.map((t=>{const o=r[t];return void 0===o?"":null===o?p(t,e):Array.isArray(o)?0===o.length&&"bracket-separator"===e.arrayFormat?p(t,e)+"[]":o.reduce(n(t),[]).join("&"):p(t,e)+"="+p(o,e)})).filter((r=>r.length>0)).join("&")},r.parseUrl=(r,e)=>{e=Object.assign({decode:!0},e);const[t,n]=o(r,"#");return Object.assign({url:t.split("?")[0]||"",query:b(m(r),e)},e&&e.parseFragmentIdentifier&&n?{fragmentIdentifier:f(n,e)}:{})},r.stringifyUrl=(e,t)=>{t=Object.assign({encode:!0,strict:!0,[i]:!0},t);const n=y(e.url).split("?")[0]||"",o=r.extract(e.url),a=r.parse(o,{sort:!1}),c=Object.assign(a,e.query);let s=r.stringify(c,t);s&&(s=`?${s}`);let u=function(r){let e="";const t=r.indexOf("#");return-1!==t&&(e=r.slice(t)),e}(e.url);return e.fragmentIdentifier&&(u=`#${t[i]?p(e.fragmentIdentifier,t):e.fragmentIdentifier}`),`${n}${s}${u}`},r.pick=(e,t,n)=>{n=Object.assign({parseFragmentIdentifier:!0,[i]:!1},n);const{url:o,query:c,fragmentIdentifier:s}=r.parseUrl(e,n);return r.stringifyUrl({url:o,query:a(c,t),fragmentIdentifier:s},n)},r.exclude=(e,t,n)=>{const o=Array.isArray(t)?r=>!t.includes(r):(r,e)=>!t(r,e);return r.pick(e,o,n)}}(r);var l=/^pk_(test|live)_[0-9a-zA-Z]+$/;return function(){function e(r,e){if(void 0===e&&(e={}),!r)throw new Error("Secret API Key is required.");if(t=r,!l.test(t))throw new Error("Public API Key is invalid.");var t;this._publicKey=r,this._checkoutURL=e.checkoutURL||"https://checkout.smartpay.co"}return e.getSessionURL=function(e,t){if(void 0===t&&(t={}),!e)throw new Error("Checkout Session is required.");var n=t.promotionCode,o={};return n&&(o["promotion-code"]=n),r.stringifyUrl({url:e.url,query:o})},e.launchCheckout=function(r,t){if(void 0===t&&(t={}),!r)throw new Error("Session required");document.location.href=e.getSessionURL(r,t)},e}()}));
