import{j as e,B as c,N as m,u,e as E,r as l,M as g,f as C,h as v,k as b,_ as j,c as _,a as y,w as N}from"./index-fb4a4614.js";const f=`class MyExampleExtension_TS_ {
    constructor(zigbee, mqtt, state, publishEntityState, eventBus, settings, logger) {
        logger.info('Loaded  MyExampleExtension_TS_');
        mqtt.publish('example/extension', 'hello from MyExampleExtension_TS_');
        this.mqttBaseTopic = settings.get().mqtt.base_topic;
        this.eventBus = eventBus;
        this.mqtt = mqtt;
        this.eventBus.on('stateChange', this.onStateChange.bind(this), this.constructor.name);
    }

    async onStateChange(data) {
        console.log('State changed', data); // comment this out if clutters logs

        const { entity, update } = data;

        //example how to toggle state
        if (entity.ID === '0x00158d000224154d') {
            //state changed for some device (example: clicked a button)
            if (update.action === 'single') {
                const myLampIeeAddr = '0x00124b001e73227f'; // change this
                this.mqtt.onMessage(\`\${this.mqttBaseTopic}/\${myLampIeeAddr}/set\`, JSON.stringify({ state: 'toggle' }));
            }
        }
    }

    async onMQTTMessage(topic, message) {
        // console.log({topic, message});
    }

    async stop() {
        this.eventBus.removeListeners(this.constructor.name);
    }
}

module.exports = MyExampleExtension_TS_;
`,S=m.create(r=>{const{updateExtensionCode:t,onCreated:o,ts:n}=r,{t:a}=u(["extensions","common"]),s=E(),[i,x]=l.useState(`example-user-extension${n}.js`),d=f.replace(/_TS_/g,n.toString()),h=()=>{t({name:i,code:d}),o({name:i,code:d}),s.remove()};return e.jsxs(g,{isOpen:s.visible,children:[e.jsx(C,{children:a("create_new_extension")}),e.jsxs(v,{children:[e.jsx("label",{className:"form-label",children:a("extension_name_propmt")}),e.jsx("input",{type:"text",className:"form-control",value:i,onChange:p=>x(p.target.value)})]}),e.jsxs(b,{children:[e.jsx("button",{type:"button",className:"btn btn-secondary",onClick:s.remove,children:a("common:close")}),e.jsx("button",{type:"button",className:"btn btn-primary",onClick:h,children:a("common:ok")})]})]})});function M(r){return e.jsx(c,{className:"btn btn-success me-2",onClick:()=>m.show(S,{ts:Date.now(),...r}),children:e.jsx("i",{className:"fa fa-plus"})})}const T=l.lazy(()=>j(()=>import("./CodeEditor-ee543824.js"),["./CodeEditor-ee543824.js","./index-fb4a4614.js","./index-654e4a69.css"],import.meta.url));class k extends l.Component{constructor(){super(...arguments),this.state={},this.loadExtension=t=>{const{value:o}=t.target;this.setState({currentExtension:o})},this.onExtensionCodeChange=t=>{const{updateExtensionCode:o}=this.props,{currentExtension:n}=this.state;n&&o({name:n,code:t})},this.onSaveClick=()=>{const{saveExtensionCode:t}=this.props;t(this.getCurrentExtension())},this.removeExtension=()=>{const{removeExtension:t}=this.props;t(this.getCurrentExtension())}}getCurrentExtension(){const{currentExtension:t}=this.state,{extensions:o}=this.props;return o.find(n=>n.name===t)}renderControls(){const{currentExtension:t}=this.state,{extensions:o,t:n,updateExtensionCode:a}=this.props;return e.jsxs("div",{className:"row mb-2",children:[e.jsx("div",{className:"col-6",children:e.jsxs("select",{value:t,className:"form-control",onChange:this.loadExtension,children:[e.jsx("option",{hidden:!0,children:n("select_extension_to_edit")},"hidden"),o.map(({name:s})=>e.jsx("option",{value:s,children:s},s))]})}),e.jsxs("div",{className:"col-6",children:[e.jsx(M,{updateExtensionCode:a,onCreated:s=>this.setState({currentExtension:s.name})}),e.jsx(c,{prompt:!0,disabled:!t,onClick:this.removeExtension,className:"btn btn-danger me-2",children:e.jsx("i",{className:"fa fa-trash"})}),e.jsx(c,{disabled:!t,onClick:this.onSaveClick,className:"btn btn-primary",children:n("common:save")})]})]})}renderEditor(){var s;const{currentExtension:t}=this.state,{extensions:o,theme:n}=this.props,a=((s=o.find(i=>i.name===t))==null?void 0:s.code)??"";return e.jsx(T,{value:a,onChange:this.onExtensionCodeChange,theme:n})}render(){return e.jsx("div",{className:"card h-100",children:e.jsxs("div",{className:"card-body h-100",children:[this.renderControls(),this.renderEditor()]})})}}const B=["extensions","theme"],q=_(B,y)(N("extensions")(k));export{q as ConnectedExtensionsEditorPage};
