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 { SwcHTMLElementBase } from "./base/SwcHTMLElementBase.js";
let SwcForOfChildren = class SwcForOfChildren2 extends SwcHTMLElementBase {
  constructor() {
    super();
    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._masterTplNodes.length === 0)
      this.initCore();
    this.renderAll();
  }
  set swcValue(val) {
    this._swcValue = val;
  }
  get swcValue() {
    return this._swcValue;
  }
  renderTemplate() {
    return `
      <style>:host { display: contents; }</style>
      <slot id="tpl-slot" style="display:none;"></slot>
    `;
  }
  connectedCallback() {
    this.initCore();
    this.renderAll();
  }
  updateSingleRow(index, newValue) {
    const targets = this.querySelectorAll(`[slot="row-${index}"]`);
    if (targets.length > 0) {
      targets.forEach((target) => this.applyData(target, newValue, index));
    } else {
      this.renderRow(newValue, index);
    }
  }
  renderRow(item, index) {
    if (!this.shadowRoot || this._masterTplNodes.length === 0)
      return;
    const slotName = `row-${index}`;
    if (!this.shadowRoot.querySelector(`slot[name="${slotName}"]`)) {
      const slot = document.createElement("slot");
      slot.name = slotName;
      this.shadowRoot.appendChild(slot);
    }
    const currentNodes = [];
    this._masterTplNodes.forEach((tplNode) => {
      const clone = tplNode.cloneNode(true);
      if (clone.nodeType === Node.ELEMENT_NODE) {
        clone.setAttribute("slot", slotName);
        clone.style.display = "";
      } else if (clone.nodeType === Node.TEXT_NODE) {
        if (clone.textContent?.trim().length === 0)
          return;
        const span = document.createElement("span");
        span.setAttribute("slot", slotName);
        span.appendChild(clone);
        this.appendChild(span);
        this.applyData(span, item, index);
        currentNodes.push(span);
        return;
      }
      this.appendChild(clone);
      this.applyData(clone, item, index);
      currentNodes.push(clone);
    });
    this._rowNodes[index] = currentNodes;
  }
  renderAll() {
    if (this._masterTplNodes.length === 0 || !this.shadowRoot)
      return;
    Array.from(this.children).forEach((c) => {
      const slot = c.getAttribute("slot");
      if (slot && slot.startsWith("row-")) {
        c.remove();
      }
    });
    Array.from(this.shadowRoot.querySelectorAll("slot")).forEach((s) => {
      if (s.id !== "tpl-slot")
        s.remove();
    });
    this._rowNodes = [];
    this._swcValue.forEach((item, index) => this.renderRow(item, index));
  }
};
__decorate([
  attribute("swc-value"),
  __metadata("design:type", Array)
], SwcForOfChildren.prototype, "_swcValue", void 0);
__decorate([
  onAttributeChanged("swc-value"),
  __metadata("design:type", Function),
  __metadata("design:paramtypes", []),
  __metadata("design:returntype", void 0)
], SwcForOfChildren.prototype, "onValueChange", null);
__decorate([
  innerHtml({ useShadow: true }),
  __metadata("design:type", Function),
  __metadata("design:paramtypes", []),
  __metadata("design:returntype", void 0)
], SwcForOfChildren.prototype, "renderTemplate", null);
SwcForOfChildren = __decorate([
  elementDefine({ name: "swc-for-of-children" }),
  __metadata("design:paramtypes", [])
], SwcForOfChildren);
export {
  SwcForOfChildren
};
//# sourceMappingURL=SwcForOfChildren.js.map
