function e(e){return typeof e==`object`&&!!e&&!Array.isArray(e)}function t(e,t=``){return typeof e==`string`?e:t}function n(e){return typeof e==`string`&&e!==``?e:null}function r(e){return typeof e==`number`&&Number.isFinite(e)?e:typeof e==`string`&&/^-?\d+(\.\d+)?$/.test(e.trim())?Number(e):null}var i=[`STREAMER`,`MODERATOR`,`VIP`,`SUB_T1`,`SUB_T2`,`SUB_T3`,`VERIFIED`,`GUEST`],a={BROADCASTER:`STREAMER`,OWNER:`STREAMER`,CREATOR:`STREAMER`,MOD:`MODERATOR`,SUBSCRIBER:`SUB_T1`,SUB:`SUB_T1`,SUB_TIER1:`SUB_T1`,SUB_TIER2:`SUB_T2`,SUB_TIER3:`SUB_T3`};function o(e){if(typeof e!=`string`)return null;let t=e.toUpperCase();return i.includes(t)?t:a[t]??null}function s(e){if(!Array.isArray(e))return[];let t=[];for(let n of e){let e=o(n);e&&!t.includes(e)&&t.push(e)}return t}var c=[`STREAMER`,`MODERATOR`,`ADMIN`,`VIP`,`SUBSCRIBER`,`VIEWER`,`GUEST`],l={BROADCASTER:`STREAMER`,OWNER:`STREAMER`,CREATOR:`STREAMER`,MOD:`MODERATOR`,USER:`VIEWER`};function u(e){if(typeof e!=`string`)return`VIEWER`;let t=e.toUpperCase();return c.includes(t)?t:l[t]??`VIEWER`}var d=/^#[0-9a-f]{6}$/i;function f(e){return typeof e==`string`&&d.test(e)?e.toUpperCase():null}function p(e){return typeof e!=`string`||e.length===0||e.length>2048?null:e.startsWith(`/`)&&!e.startsWith(`//`)||/^https?:\/\//i.test(e)?e:null}function m(t){let n=e(t)?t:{},i=r(n.slowSec),a=r(n.followersMin);return{slowSec:i!==null&&i>0?Math.round(i):0,followersMin:a!==null&&a>=0?Math.round(a):null,subsOnly:n.subsOnly===!0,blockLinks:n.blockLinks===!0,guestChat:n.guestChat===!0}}var h=[`OPEN`,`READ_ONLY`,`CLOSED`,`NOT_OPEN`];function g(e){return typeof e==`string`&&h.includes(e.toUpperCase())?e.toUpperCase():null}function _(t){let n=e(t)?t:{},r=n.pinned?T(n.pinned):null;return{status:g(n.status)??`OPEN`,mode:m(n.mode),pinned:r&&!r.deleted?r:null}}function v(t){return!e(t)||t.type!==`BAN`&&t.type!==`TIMEOUT`?null:{type:t.type,until:n(t.until)}}function y(e){let t=r(e);return t!==null&&t>=0&&t<=3?Math.trunc(t):0}function b(t){if(!e(t))return null;let i=e(t.details)?t.details:{},a=u(t.role),o=v(t.restriction);return{role:a,badges:s(t.badges),color:f(t.color),canSend:t.canSend===!0,reason:n(t.reason),retryAfterSec:r(t.retryAfterSec)??r(i.retryAfterSec),until:n(t.until)??n(i.until)??(o?.type===`TIMEOUT`?o.until:null),requiredMin:r(t.requiredMin)??r(i.requiredMin),canModerate:typeof t.canModerate==`boolean`?t.canModerate:a===`STREAMER`||a===`MODERATOR`||a===`ADMIN`,restriction:o,subTier:y(t.subTier),emoteTier:y(t.emoteTier)}}function x(t){let n=e(t)?t:{};return{room:_(n.room),me:b(n.me)}}function S(r){if(!e(r))return null;let i=t(r.text);if(r.type===`emote`&&e(r.emote)){let e=p(r.emote.imageUrl??r.emote.url),n=t(r.emote.code,i);return e&&n?{type:`emote`,text:i||n,emote:{id:t(r.emote.id,n),code:n,imageUrl:e}}:{type:`text`,text:i||n}}return r.type===`mention`?i?{type:`mention`,text:i,userId:n(r.userId)}:null:i?{type:`text`,text:i}:null}function C(n){if(!e(n))return null;let r=t(n.id),i=t(n.username);return r?{id:r,username:i,displayName:t(n.displayName)||i||r.slice(0,8),color:f(n.color),badges:s(n.badges)}:null}var w={id:`system`,username:``,displayName:``,color:null,badges:[]};function T(r){if(!e(r))return null;let i=t(r.id);if(!i)return null;let a=t(r.type).toUpperCase()===`NORMAL`||!r.type?`NORMAL`:`SYSTEM`,o=C(r.user)??(a===`SYSTEM`?w:null);if(!o)return null;let s=t(r.body),c=Array.isArray(r.fragments)?r.fragments.map(S).filter(e=>e!==null):[],l=e(r.flags)?r.flags:{};return{id:i,streamId:t(r.streamId),user:o,body:s,fragments:c.length>0?c:s?[{type:`text`,text:s}]:[],type:a,createdAt:t(r.createdAt),deleted:r.deleted===!0||l.deleted===!0||typeof r.deletedAt==`string`,pinned:r.pinned===!0,clientMsgId:n(r.clientMsgId)}}function E(t){let n=e(t)?t:{};return(Array.isArray(n.items)?n.items:Array.isArray(n.messages)?n.messages:[]).map(T).filter(e=>e!==null).reverse()}function D(i){let a=e(i)?i:{},o=Array.isArray(a.items)?a.items:[],s=[];for(let i of o){if(!e(i))continue;let a=t(i.code),o=p(i.imageUrl??i.url);if(!a||!o)continue;let c=r(i.tierRequired);s.push({id:t(i.id,a),code:a,imageUrl:o,animated:i.animated===!0,scope:i.scope===`CHANNEL`||n(i.channelId)?`CHANNEL`:`GLOBAL`,channelId:n(i.channelId),tierRequired:c!==null&&c>=1&&c<=3?Math.trunc(c):null})}return s}function O(t){let r=e(t)?t:{};return{...m(r),updatedAt:n(r.updatedAt)}}function k(e){let i=r(e.quantity);return!t(e.username)||!t(e.itemName)||i===null||i<1?null:{userId:t(e.userId),username:t(e.username),itemName:t(e.itemName),icon:t(e.icon),quantity:Math.trunc(i),total:Math.max(0,Math.trunc(r(e.total)??0)),message:n(e.message)}}function A(n){if(!e(n))return null;let i=t(n.text),a=t(n.type??n.kind).toLowerCase(),o=e(n.meta)?n.meta:{},s=r(o.tier),c=(a===`sub_new`||a===`sub_renew`)&&t(o.username)&&s!==null&&s>=1&&s<=3?{kind:a===`sub_new`?`NEW`:`RENEW`,userId:t(o.userId),username:t(o.username),tier:Math.trunc(s)}:null,l=a===`gift`?k(o):null;return i||c||l?{kind:a,text:i,sub:c,gift:l}:null}var j=new Set([`mode_change`,`mode`,`chat_mode`]);function M(r,i){switch(r){case`message`:{let e=T(i);return e?{type:`message`,message:e}:null}case`ack`:{let n=e(i)?i:{},r=t(n.clientMsgId),a=t(n.messageId);return r&&a?{type:`ack`,clientMsgId:r,messageId:a}:null}case`system`:{let e=A(i);return!e||j.has(e.kind)?null:e.sub?{type:`system`,text:e.text,sub:e.sub}:e.gift?{type:`system`,text:e.text,gift:e.gift}:{type:`system`,text:e.text}}case`mode_change`:return{type:`mode_change`,mode:m(e(i)&&e(i.mode)?i.mode:i)};case`room_state`:{let t=g(e(i)?i.status:null);return t?{type:`room_state`,status:t}:null}case`sampling`:return{type:`sampling`,active:e(i)&&i.active===!0};case`delete`:{let n=e(i)?t(i.messageId):``;return n?{type:`delete`,messageId:n}:null}case`user_cleared`:{let r=e(i)?i:{},a=t(r.userId);return a?{type:`user_cleared`,userId:a,reason:r.reason===`ban`?`ban`:`timeout`,until:n(r.until)}:null}case`user_restored`:{let n=e(i)?t(i.userId):``;return n?{type:`user_restored`,userId:n}:null}case`pin`:{let t=T(e(i)?i.message:null);return t?{type:`pin`,message:t}:null}case`unpin`:return{type:`unpin`};default:return null}}export{E as a,D as i,M as n,T as o,x as r,O as s,o as t};