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, onAttributeChanged } from "../index.js";
import { SwcHTMLDetailsElementBase } from "../elements/base/SwcHTMLDetailsElementBase.js";
let SwcChooseChildrenDetails = class SwcChooseChildrenDetails2 extends SwcHTMLDetailsElementBase {
  constructor() {
    super(...arguments);
    this._observer = null;
    this._swcValue = void 0;
    this._branches = [];
    this._otherBranch = null;
    this._isInitialized = false;
  }
  onValueChange() {
    this.evaluate();
  }
  set swcValue(val) {
    this._swcValue = val;
  }
  get swcValue() {
    return this._swcValue;
  }
  connectedCallback() {
    this._initBranches();
    this._observer = new MutationObserver(() => this.evaluate());
    this._observer.observe(this, { attributes: true });
    this.evaluate();
  }
  _initBranches() {
    if (this._isInitialized)
      return;
    Array.from(this.children).forEach((c) => {
      const isWhen = c.localName === "swc-when" || c.getAttribute("is")?.startsWith("swc-when-");
      const isOther = c.localName === "swc-other" || c.getAttribute("is")?.startsWith("swc-other-");
      const branch = { tpl: c.cloneNode(true), test: c.getAttribute("test") };
      if (isWhen)
        this._branches.push(branch);
      else if (isOther)
        this._otherBranch = branch;
    });
    this.innerHTML = "";
    this._isInitialized = true;
  }
  disconnectedCallback() {
    this._observer?.disconnect();
  }
  evaluate() {
    if (!this._isInitialized)
      return;
    let matched = false;
    const chooseValue = this.getAttribute("swc-value") ?? this.getAttribute("value") ?? this._swcValue;
    this.innerHTML = "";
    for (const branch of this._branches) {
      if (matched)
        break;
      const testAttr = branch.test;
      if (testAttr === null || testAttr.includes("{{"))
        continue;
      let isMet = false;
      if (chooseValue !== void 0 && chooseValue !== null) {
        isMet = String(chooseValue) === String(testAttr);
      } else {
        try {
          if (testAttr === "true")
            isMet = true;
          else if (testAttr === "false")
            isMet = false;
          else
            isMet = !!new Function("return " + testAttr)();
        } catch (e) {
          isMet = false;
        }
      }
      if (isMet) {
        this._renderBranch(branch);
        matched = true;
      }
    }
    if (!matched && this._otherBranch) {
      this._renderBranch(this._otherBranch);
    }
  }
  _renderBranch(branch) {
    const clone = branch.tpl.cloneNode(true);
    clone.style.display = "contents";
    this.appendChild(clone);
    this.applyData(clone, this._swcValue);
  }
};
__decorate([
  attribute("swc-value"),
  __metadata("design:type", Object)
], SwcChooseChildrenDetails.prototype, "_swcValue", void 0);
__decorate([
  onAttributeChanged("swc-value"),
  __metadata("design:type", Function),
  __metadata("design:paramtypes", []),
  __metadata("design:returntype", void 0)
], SwcChooseChildrenDetails.prototype, "onValueChange", null);
SwcChooseChildrenDetails = __decorate([
  elementDefine({ name: "swc-choose-children-details", extends: "details" })
], SwcChooseChildrenDetails);
export {
  SwcChooseChildrenDetails
};
//# sourceMappingURL=SwcChooseChildrenDetails.js.map
