(function(p,m){typeof exports=="object"&&typeof module<"u"?m(exports):typeof define=="function"&&define.amd?define(["exports"],m):(p=typeof globalThis<"u"?globalThis:p||self,m(p.xmltv={}))})(this,function(p){"use strict";function m(e){e=e.replace(/[\s]/g,"");let t,n;const i=e.replace(/\D+/g,"");if(i.length<=8){if(i.length===8){const T=parseInt(i.slice(0,4),10),N=parseInt(i.slice(4,6),10)-1||0,D=parseInt(i.slice(6,8),10)||1;return new Date(Date.UTC(T,N,D))}if(i.length===6){const T=parseInt(i.slice(0,4),10),N=parseInt(i.slice(4,6),10)-1||0;return new Date(Date.UTC(T,N))}if(i.length===4)return new Date(i)}e.endsWith("Z")?(t=e.slice(0,-1),n=null):(t=e.slice(0,-5),n=e.slice(-5));const a=t.replace(/[-:Zz]/g,""),s=parseInt(a.slice(0,4),10),r=parseInt(a.slice(4,6),10)-1||0,o=parseInt(a.slice(6,8),10)||1,c=parseInt(a.slice(8,10),10)||0,g=parseInt(a.slice(10,12),10)||0,w=parseInt(a.slice(12,14),10)||0,A=new Date(Date.UTC(s,r,o,c,g,w));if(n){const T=n.slice(0,1),N=parseInt(n.slice(1,3),10),D=parseInt(n.slice(3,5),10),U=N*60+D;T==="+"?A.setUTCMinutes(A.getUTCMinutes()-U):T==="-"&&A.setUTCMinutes(A.getUTCMinutes()+U)}return A}function O(e){const t=c=>c<10?`0${c}`:c.toString(),n=t(e.getUTCFullYear()),i=t(e.getUTCMonth()+1),a=t(e.getUTCDate()),s=t(e.getUTCHours()),r=t(e.getUTCMinutes()),o=t(e.getUTCSeconds());return`${n}${i}${a}${s}${r}${o} +0000`}function z(e){const t=new Map;for(const[n,i]of e.entries())t.set(i,n);return t}const B=["actor","adapter","aspect","audio","category","channel","colour","commentator","composer","country","credits","date","desc","director","display-name","editor","episode-num","guest","icon","image","keyword","language","last-chance","length","new","orig-language","premiere","present","presenter","previously-shown","producer","programme","quality","rating","review","star-rating","stereo","sub-title","subtitles","title","tv","url","value","video","writer"],I=["channel","clumpidx","date","generator-info-name","generator-info-url","guest","height","id","lang","orient","pdc-start","reviewer","role","showview","size","source-data-url","source-info-name","source-info-url","source","src","start","stop","system","type","units","videoplus","vps-start","width"],M=new Map([...B.map(e=>[e,e])]),E=new Map([...I.map(e=>[e,e])]),X=z(M),q=z(E);function C(e,t){if(!B.includes(e))throw new Error(`Invalid tag: ${e}`);const n=M,i=X;n&&i&&(n.set(e,t),i.set(t,e))}function y(e,t){if(!I.includes(e))throw new Error(`Invalid attribute: ${e}`);const n=E,i=q;n&&i&&(n.set(e,t),i.set(t,e))}C("display-name","displayName"),C("episode-num","episodeNum"),C("last-chance","lastChance"),C("orig-language","origLanguage"),C("previously-shown","previouslyShown"),C("star-rating","starRating"),C("sub-title","subTitle"),C("channel","channels"),C("programme","programmes"),y("generator-info-name","generatorInfoName"),y("generator-info-url","generatorInfoUrl"),y("pdc-start","pdcStart"),y("vps-start","vpsStart"),y("source-data-url","sourceDataUrl"),y("source-info-name","sourceInfoName"),y("source-info-url","sourceInfoUrl");function v(e,t="tv",n=!1){if(Array.isArray(e))return e.map(s=>v(s,t,!0));if(typeof e=="number")return e.toString();if(typeof e=="string")return e;if(e instanceof Date&&t!=="date")return O(e);if(typeof e=="boolean"&&t!=="new")return e?"yes":"no";const i=X.get(t)||t,a={tagName:i,attributes:{},children:[]};for(let s in e){const r=q.get(s)||s;if(e[s].tagName==="new"){e[i].children=[];continue}if(I.indexOf(r)>=0&&typeof e[s]!="object"||e[s]instanceof Date){if(a.tagName==="credits"&&s==="guest")continue;if(a.tagName==="programme"&&s==="channel"){a.attributes[r]=e[s];continue}if(a.tagName==="tv"&&s==="date"){a.attributes[r]=O(e[s]);continue}if(a.tagName==="programme"&&s==="date"){a.children.push({tagName:r,attributes:{},children:[O(e[s])]});continue}const o=typeof e[s];if(o==="number"){a.attributes[r]=e[s].toString();continue}if(o==="boolean"){a.attributes[r]=e[s]?"yes":"no";continue}if(o==="object"&&!Array.isArray(e[s])){if(e[s]instanceof Date){e[s]=O(e[s]),a.attributes[r]=e[s];continue}const c=Object.keys(e[s]).map(g=>{e[s][g]=e[s][g].toString()});a.attributes[r]=c;continue}a.attributes[r]=e[s]}else{const o=v(e[s],s);if(Array.isArray(o))for(let c=0,g=o.length;c/= `,U=["new","icon","previously-shown"];function Z(l){const f=[];for(;e[t];)if(e.charCodeAt(t)==a){if(e.charCodeAt(t+1)===o){const h=t+2;if(t=e.indexOf(i,t),e.substring(h,t).indexOf(l)==-1){const u=e.substring(0,t).split(` `);throw new Error(`Unexpected close tag Line: `+(u.length-1)+` Column: `+(u[u.length-1].length+1)+` Char: `+e[t])}return t+1&&(t+=1),f}else if(e.charCodeAt(t+1)===c){if(e.charCodeAt(t+2)==r){for(;t!==-1&&!(e.charCodeAt(t)===s&&e.charCodeAt(t-1)==r&&e.charCodeAt(t-2)==r&&t!=-1);)t=e.indexOf(i,t+1);t===-1&&(t=e.length)}else{const h=t+1;t+=2;let b=!1;for(;(e.charCodeAt(t)!==s||b===!0)&&e[t];)e.charCodeAt(t)===A?b=!0:b===!0&&e.charCodeAt(t)===T&&(b=!1),t++;f.push(e.substring(h,t))}t++;continue}const d=K();if(f.push(d),d.tagName.charCodeAt(0)===N&&d.children){for(let h=0,b=d.children.length;h0&&f.push(d),t++}return f}function J(){const l=t;return t=e.indexOf(n,t)-1,t===-2&&(t=e.length),e.slice(l,t+1)}function L(){const l=t;for(;D.indexOf(e[t])===-1&&e[t];)t++;return e.slice(l,t)}function K(){t++;const l=L(),f={};let d=[];for(;e.charCodeAt(t)!==s&&e[t];){const h=e.charCodeAt(t);if(h>64&&h<91||h>96&&h<123){const b=L();let u=e.charCodeAt(t),_;for(;u&&u!==g&&u!==w&&!(u>64&&u<91||u>96&&u<123)&&u!==s;)t++,u=e.charCodeAt(t);if(u===g||u===w){if(_=V(),t===-1)return{tagName:l,attributes:f,children:d}}else _=null,t--;f[b]=_}t++}return e.charCodeAt(t-1)!==o&&U.indexOf(l)===-1?(t++,d=Z(l)):t++,l==="date"?{tagName:l,attributes:f,children:d.map(h=>O(m(h)))}:{tagName:l,attributes:f,children:d}}function V(){const l=e[t],f=t+1;return t=e.indexOf(l,f),e.slice(f,t)}return Z("")}const R=63,k=["credits","date","language","orig-language","length","country","previously-shown","premiere","last-chance","new","video","audio","present","colour","aspect","quality","present","stereo","value"],F=["date","value","aspect","present","colour","quality","stereo"];function Y(e,t={tagName:"tv",attributes:{},children:[]}){let n={};if(!e.length)return n;if(e.length===1&&typeof e[0]=="string"&&(e[0]==="yes"||e[0]==="no"))return e[0]==="yes";if(e.length===1&&typeof e[0]=="string"&&typeof t!="string")return Object.keys(t.attributes).length?{_value:e[0]}:e[0];for(let i=0,a=e.length;i(g[E.get(w)||w]=s.attributes[w],g),{});Object.assign(o,c)}if(r==="subtitles"){typeof o.language=="string"&&(o.language={_value:o.language}),n[r].push(o);continue}if(r==="tv"){n=o;continue}if(r==="date"){n[r]=m(o);continue}if(typeof o=="string"&&F.indexOf(s.tagName)===-1&&(o={_value:o}),Array.isArray(n[r])){n[r].push(o);continue}n[r]=o}return n}function $(e){let t="";function n(s){if(s)for(var r=0;r";continue}t+=s[r].trim()}else i(s[r])}function i(s){if(typeof s!="string"){t+="<"+s.tagName;for(const r in s.attributes)typeof s.attributes[r]=="string"&&s.attributes[r].indexOf('"')===-1?t+=" "+r+'="'+s.attributes[r].trim()+'"':typeof s.attributes[r]=="boolean"?t+=" "+r+'="'+(s.attributes[r]?"yes":"no")+'"':t+=" "+r+"='"+s.attributes[r]+"'";if(["new","icon","previously-shown"].indexOf(s.tagName)>=0){t+="/>";return}if(s.tagName==="?xml"){t+="?>";return}if(t+=">",typeof s.children=="boolean"){t+=s.children?"yes":"no";return}n(s.children),t+=""}}n(e);let a="";return t.indexOf("?xml")===-1&&(a+=''),t.indexOf("!DOCTYPE")===-1&&(a+=''),a+t}function H(e,t={asDom:!1}){const n=P(e);return t.asDom?n:Y(n)}function Q(e,t={fromDom:!1}){if(t.fromDom){if(typeof e=="object"&&!Array.isArray(e))throw new Error("Cannot write XMLTV from a DOM object that has been converted to an object");return $(e)}const n=v(e);return $(n)}p.addAttributeTranslation=y,p.addTagTranslation=C,p.objectToDom=v,p.parseXmltv=H,p.parser=P,p.writeXmltv=Q,p.writer=$,Object.defineProperty(p,Symbol.toStringTag,{value:"Module"})});