import{d as _,a as c,a9 as o,aa as n,T as y,a6 as k,u as h,ac as m,a7 as p,a8 as f}from"./app-CPb3bToC.js";const N=_("model_kb_by_v2_keys",{state:()=>({keys:[],macros:[],layers:[],current_layer:0,product_name:""}),actions:{reset(){const r=c();if(!r.current_device)return null;let e=o(),s=[];for(let t=0;t<e.length;t++){let i=[],a=n(e[t].value);for(let l=0;l<a.length;l++)a[l].layer=e[t].value,a[l].type=y.BASIC,a[l].color={r:0,g:0,b:0,a:0},i.push(a[l]);s=s.concat(i)}this.keys=s,this.macros=[],this.layers=e,this.current_layer=0,this.product_name=r.current_device.product_name},load_profile(r){this.keys=JSON.parse(JSON.stringify(r.keys)),this.macros=JSON.parse(JSON.stringify(r.macros)),this.layers=JSON.parse(JSON.stringify(r.layers)),this.current_layer=r.current_layer,this.product_name=r.product_name},default(){const r=c();if(!r.current_device)return null;let e=o(),s=[];for(let t=0;t<e.length;t++){let i=[],a=n(e[t].value);for(let l=0;l<a.length;l++)a[l].layer=e[t].value,a[l].type=y.BASIC,a[l].color={r:0,g:0,b:0,a:0},i.push(a[l]);s=s.concat(i)}return{keys:s,macros:[],layers:e,current_layer:0,product_name:r.current_device.product_name}},get_current_layer_keys_by_pos(r){for(let e=0;e<this.keys.length;e++)if(Number(this.keys[e].layer)===Number(this.current_layer)&&Number(this.keys[e].pos)===Number(r))return JSON.parse(JSON.stringify(this.keys[e]));return null},get_current_layer_keys(){let r=[];for(let e=0;e<this.keys.length;e++)Number(this.keys[e].layer)===Number(this.current_layer)&&r.push(this.keys[e]);return r},set_key(r,e){for(let s=0;s<this.keys.length;s++)this.keys[s].type===y.MACRO&&this.keys[s].name===e.name&&(this.keys[s].value=JSON.parse(JSON.stringify(e.data.data))),Number(this.keys[s].pos)===Number(r)&&Number(this.keys[s].layer)===Number(this.current_layer)&&(this.keys[s].type=e.type,this.keys[s].value=e.data,e.type===y.MACRO&&(this.keys[s].value=JSON.parse(JSON.stringify(e.data.data))),this.keys[s].name=e.name);this._sync_macros()},_sync_data_to_device(){c().set_device_data(this)},_sync_macros(){let r=[];for(let e=0;e<this.keys.length;e++)this.keys[e].type===y.MACRO&&r.push(JSON.parse(JSON.stringify(this.keys[e].value)));r=k(r,"name"),this.macros=JSON.parse(JSON.stringify(r)),this._sync_data_to_device()},reset_key(r){let e=n(this.current_layer);for(let s=0;s<this.keys.length;s++)for(let t=0;t<e.length;t++)if(Number(this.keys[s].pos)===Number(r)&&Number(e[t].pos)===Number(r)&&Number(this.keys[s].layer)===Number(this.current_layer)){e[t].layer=this.current_layer,e[t].type=y.BASIC,e[t].color=this.keys[s].color,this.keys[s]=JSON.parse(JSON.stringify(e[t]));break}this._sync_macros()},setup(){this.layers.length===0&&(this.layers=o());const r=c();if(this.keys.length===0||this.product_name!==r.current_device.product_name){let e=[];for(let s=0;s<this.layers.length;s++){let t=[],i=n(this.layers[s].value);for(let a=0;a<i.length;a++)i[a].layer=this.layers[s].value,i[a].type=y.BASIC,i[a].color={r:0,g:0,b:0,a:0},t.push(i[a]);e=e.concat(t)}this.keys=JSON.parse(JSON.stringify(e)),this.product_name=r.current_device.product_name}},set_custom_light(r,e){for(let s=0;s<this.keys.length;s++)if(Number(this.keys[s].layer)===0&&Number(this.keys[s].light_pos)===Number(r))return this.keys[s].color={r:e.r,g:e.g,b:e.b,a:e.a},!0},async sync(){try{return await(await h().controller).set_keys(this)}catch{return!1}},async sync_custom_light(){try{return await(await h().controller).set_custom_light(this)}catch{return!1}},async fetch(){try{const s=await(await h().controller).get_keys(this.current_layer);for(let t=0;t<this.keys.length;t++)if(Number(this.keys[t].layer)===Number(this.current_layer)){const i=Number(this.keys[t].pos-8);if(this.keys[t].name="",this.keys[t].value="0x00000000",this.keys[t].type=y.BASIC,s[i+0]===3){let a=s[i+3];if(a>this.macros.length-1&&(a=0),this.macros.length===0){let u=m(0,Number(this.keys[t].pos-8));this.keys[t].name=u.name,this.keys[t].value=u.value,this.keys[t].type=y.BASIC;continue}let l=JSON.parse(JSON.stringify(this.macros[a]));this.keys[t].name=l.name,this.keys[t].value=JSON.parse(JSON.stringify(l)),this.keys[t].type="macro"}else{let a=0;a+=s[i+0]*Math.pow(256,3),a+=s[i+1]*Math.pow(256,2),a+=s[i+2]*Math.pow(256,1),a+=s[i+3]*Math.pow(256,0),this.keys[t].value="0x"+p(a,8);const l=f(this.keys[t].value);try{this.keys[t].name=l.name,this.keys[t].type=l.type,a===0&&(this.keys[t].name="")}catch{a===0&&(this.keys[t].name="");continue}}}return this._sync_data_to_device(),!0}catch{return!1}},async fetch_custom_light(){try{const s=await(await h().controller).get_custom_light();for(let t=0;t<this.keys.length;t++)if(this.keys[t].layer===0){let i={r:0,g:0,b:0,a:1};i.r=s[Number(this.keys[t].light_pos)-8],i.g=s[Number(this.keys[t].light_pos)+126-8],i.b=s[Number(this.keys[t].light_pos)+126*2-8],i.r===0&&i.g===0&&i.b===0&&(i.a=0),this.keys[t].color=JSON.parse(JSON.stringify(i))}return!0}catch{return!1}}},persist:!0});export{N as m};
