var __decorate = function(decorators, target, key, desc) {
  var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
  if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
  else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
  return c > 3 && r && Object.defineProperty(target, key, r), r;
};
var __metadata = function(k, v) {
  if (typeof Reflect === "object" && typeof Reflect.metadata === "function") return Reflect.metadata(k, v);
};
import { attribute, elementDefine, innerHtml, onAttributeChanged } from "../index.js";
import { SwcHTMLTextAreaElementBase } from "../elements/base/SwcHTMLTextAreaElementBase.js";
let SwcForOfTextArea = class SwcForOfTextArea2 extends SwcHTMLTextAreaElementBase {
  constructor() {
    super(...arguments);
    this._placeholder = document.createComment("swc-for-of-placeholder");
    this._isShowing = true;
    this._isConnected = false;
    this._swcValue = [];
    this._rowNodes = [];
  }
  onValueChange() {
    if (!Array.isArray(this._swcValue))
      this._swcValue = [];
    if (!this._swcValue.__swc_is_proxy) {
      this._swcValue = this.createReactiveProxy(this._swcValue, () => this.renderAll(), (idx, v) => this.updateSingleRow(idx, v));
    }
    if (this._isConnected) {
      if (this._masterTplNodes.length === 0)
        this.initCore();
      this.evaluate();
    }
  }
  set swcValue(val) {
    this._swcValue = val;
  }
  get swcValue() {
    return this._swcValue;
  }
  renderTemplate() {
    return `<slot id="tpl-slot" style="display:none;"></slot>`;
  }
  connectedCallback() {
    this._isConnected = true;
    this.initCore();
    this.evaluate();
  }
  evaluate() {
    if (this._masterTplNodes.length === 0)
      this.initCore();
    const hasData = Array.isArray(this._swcValue) && this._swcValue.length > 0;
    if (!hasData && this._isShowing) {
      if (this.parentNode) {
        this.parentNode.insertBefore(this._placeholder, this);
        this.remove();
        this._isShowing = false;
      }
    } else if (hasData) {
      if (!this._isShowing) {
        if (this._placeholder.parentNode) {
          this._placeholder.parentNode.insertBefore(this, this._placeholder);
          this._placeholder.remove();
          this._isShowing = true;
        }
      }
      this.renderAll();
    }
  }
  updateSingleRow(index, newValue) {
    if (!this._isShowing)
      return;
    const nodes = this._rowNodes[index];
    if (nodes && nodes.length > 0) {
      nodes.forEach((node) => this.applyData(node, newValue, index));
    } else {
      this.renderRow(newValue, index);
    }
  }
  addRow(item) {
    this._swcValue.push(item);
  }
  removeRow(index) {
    if (index < 0 || index >= this._rowNodes.length)
      return;
    this._rowNodes[index].forEach((node) => node.remove());
    this._rowNodes.splice(index, 1);
    this._swcValue.splice(index, 1);
    for (let i = index; i < this._rowNodes.length; i++) {
      this._rowNodes[i].forEach((node) => this.applyData(node, this._swcValue[i], i));
    }
    this.evaluate();
  }
  renderRow(item, index) {
    if (this._masterTplNodes.length === 0)
      return;
    const currentNodes = [];
    this._masterTplNodes.forEach((tplNode) => {
      const clone = tplNode.cloneNode(true);
      this.appendChild(clone);
      this.applyData(clone, item, index);
      currentNodes.push(clone);
    });
    this._rowNodes[index] = currentNodes;
  }
  renderAll() {
    this.innerHTML = "";
    this._rowNodes = [];
    this._swcValue.forEach((item, index) => this.renderRow(item, index));
  }
};
__decorate([
  attribute("swc-value"),
  __metadata("design:type", Array)
], SwcForOfTextArea.prototype, "_swcValue", void 0);
__decorate([
  onAttributeChanged("swc-value"),
  __metadata("design:type", Function),
  __metadata("design:paramtypes", []),
  __metadata("design:returntype", void 0)
], SwcForOfTextArea.prototype, "onValueChange", null);
__decorate([
  innerHtml,
  __metadata("design:type", Function),
  __metadata("design:paramtypes", []),
  __metadata("design:returntype", void 0)
], SwcForOfTextArea.prototype, "renderTemplate", null);
SwcForOfTextArea = __decorate([
  elementDefine({ name: "swc-for-of-textarea", extends: "textarea" })
], SwcForOfTextArea);
export {
  SwcForOfTextArea
};
//# sourceMappingURL=SwcForOfTextArea.js.map
