import util from"../common/utils/utils";class Store{constructor(t){this.state={...t},this.callbacks={}}get(t){if(util.isString(t))return this._getByStr(t);if(util.isObj(t)){const s={...t};Object.keys(s).forEach((t=>{const e=s[t];this.callbacks[t]||(this.callbacks[t]=[]),this.callbacks[t].indexOf(e)<0&&this.callbacks[t].push(e);e(this._getByStr(t))}));return()=>{Object.keys(s).forEach((t=>{const e=s[t],i=this.callbacks[t].indexOf(e);this.callbacks[t].splice(i,1)}))}}throw Error("key must be string or object.")}set(t,s){if(util.isObj(t)){const s={...t};Object.keys(s).forEach((t=>{const e=s[t];this._setItem(t,e)}))}else this._setItem(t,s)}reset(){this.state.main={chat:null,channelId:"",userInfo:"",channelDetail:{},chapterList:[],videoId:"",videoPoolId:"",imageId:""}}_getItem(t,s){this.callbacks[t]||(this.callbacks[t]=[]),this.callbacks[t].push(s);const e=this._getByStr(t);return s(e),e}_setItem(t,s){const e=t.split(".");s!==this._getByStr(t)&&(this._setByStr(t,s),e.reduce(((t,s)=>{const e=t?t+"."+s:s;(this.callbacks[e]||[]).forEach((t=>{t(this._getByStr(e))}))})))}_getByStr(t){if(!t)return this.state;const s=t.split(".");return s.reduce(((t,e,i)=>{if(!t[e]&&i<s.length-1)throw Error(`${e} is undefined in state`);return t[e]}),this.state)}_setByStr(t,s){const e=t.split(".");e.reduce(((t,i,a)=>(a===e.length-1&&(t[i]=s),t[i]=t[i]||{},t[i])),this.state)}}const initialState={app:{apiId:"",verifyUrl:"",userId:""},main:{chat:null,channelId:"",userInfo:"",channelDetail:{},chapterList:[],openId:"",userName:"",avatarUrl:"",imageId:"",screenType:"half"}};export default new Store(initialState);