var __defProp = Object.defineProperty;
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
var __decorateClass = (decorators, target, key, kind) => {
  var result = kind > 1 ? void 0 : kind ? __getOwnPropDesc(target, key) : target;
  for (var i = decorators.length - 1, decorator; i >= 0; i--)
    if (decorator = decorators[i])
      result = (kind ? decorator(target, key, result) : decorator(result)) || result;
  if (kind && result) __defProp(target, key, result);
  return result;
};
/**
 * @license
 * Copyright 2025-2026 Open Home Foundation
 * SPDX-License-Identifier: Apache-2.0
 */
import "@material/web/button/filled-button";
import "@material/web/textfield/outlined-text-field";
import { css, html, nothing } from "lit";
import { customElement, state } from "lit/decorators.js";
import { showAlertDialog } from "../../../components/dialog-box/show-dialog-box.js";
import { handleAsync } from "../../../util/async-handler.js";
import { BaseClusterCommands } from "../base-cluster-commands.js";
import { registerClusterCommands } from "../registry.js";
const CLUSTER_ID = 40;
const NODE_LABEL_ATTRIBUTE_ID = 5;
const MAX_NODE_LABEL_LENGTH = 32;
let BasicInformationClusterCommands = class extends BaseClusterCommands {
  constructor() {
    super(...arguments);
    this._nodeLabel = "";
    this._saving = false;
  }
  updated(changedProperties) {
    super.updated(changedProperties);
    if (changedProperties.has("node") && this.node) {
      this._loadCurrentNodeLabel();
    }
  }
  /**
   * Load the current NodeLabel from the node's cached attributes.
   */
  _loadCurrentNodeLabel() {
    if (!this.node) {
      return;
    }
    if (this.node.nodeLabel) {
      this._nodeLabel = this.node.nodeLabel;
      return;
    }
    const attributePath = `0/${CLUSTER_ID}/${NODE_LABEL_ATTRIBUTE_ID}`;
    const currentValue = this.node.attributes[attributePath];
    if (typeof currentValue === "string") {
      this._nodeLabel = currentValue;
    } else {
      this._nodeLabel = "";
    }
  }
  /**
   * Check if the node is available (not offline).
   */
  get _isNodeAvailable() {
    return this.node.available;
  }
  render() {
    return html`
            <details class="command-panel">
                <summary>Node Label</summary>
                <div class="command-content">
                    ${!this._isNodeAvailable ? html` <div class="offline-warning">Node is offline - cannot edit label</div> ` : nothing}
                    <div class="command-row">
                        <md-outlined-text-field
                            label="Node Label"
                            .value=${this._nodeLabel}
                            @input=${this._handleInput}
                            maxlength=${MAX_NODE_LABEL_LENGTH}
                            ?disabled=${!this._isNodeAvailable || this._saving}
                            supporting-text="Max ${MAX_NODE_LABEL_LENGTH} characters"
                        ></md-outlined-text-field>
                        <md-filled-button
                            @click=${handleAsync(() => this._handleSave())}
                            ?disabled=${!this._isNodeAvailable || this._saving}
                        >
                            ${this._saving ? "Saving..." : "Save"}
                        </md-filled-button>
                    </div>
                </div>
            </details>
        `;
  }
  _handleInput(e) {
    const input = e.target;
    this._nodeLabel = input.value;
  }
  async _handleSave() {
    if (!this._isNodeAvailable) {
      showAlertDialog({
        title: "Cannot Save",
        text: "Node is offline. Please wait until the node is available."
      });
      return;
    }
    this._saving = true;
    try {
      const attributePath = `0/${CLUSTER_ID}/${NODE_LABEL_ATTRIBUTE_ID}`;
      await this.client.writeAttribute(this.node.node_id, attributePath, this._nodeLabel);
      showAlertDialog({
        title: "Success",
        text: `Node label set to "${this._nodeLabel}"`
      });
    } catch (error) {
      const errorMessage = error instanceof Error ? error.message : String(error);
      showAlertDialog({
        title: "Failed to set node label",
        text: errorMessage
      });
    } finally {
      this._saving = false;
    }
  }
};
BasicInformationClusterCommands.styles = [
  BaseClusterCommands.styles,
  css`
            .command-row {
                align-items: flex-start;
            }

            md-outlined-text-field {
                width: 36ch; /* 32 chars + some padding for field chrome */
            }

            md-filled-button {
                margin-top: 8px; /* Align with text field input area, accounting for label */
            }

            .offline-warning {
                color: var(--danger-color, #d32f2f);
                font-size: 14px;
                margin-bottom: 12px;
                padding: 8px 12px;
                background-color: var(--md-sys-color-error-container, #fdecea);
                border-radius: 4px;
            }
        `
];
__decorateClass([
  state()
], BasicInformationClusterCommands.prototype, "_nodeLabel", 2);
__decorateClass([
  state()
], BasicInformationClusterCommands.prototype, "_saving", 2);
BasicInformationClusterCommands = __decorateClass([
  customElement("basic-information-cluster-commands")
], BasicInformationClusterCommands);
registerClusterCommands(CLUSTER_ID, "basic-information-cluster-commands");
export {
  BasicInformationClusterCommands
};
//# sourceMappingURL=basic-information-commands.js.map
