var __defProp = Object.defineProperty;
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
var __getOwnPropNames = Object.getOwnPropertyNames;
var __hasOwnProp = Object.prototype.hasOwnProperty;
var __export = (target, all) => {
  for (var name in all)
    __defProp(target, name, { get: all[name], enumerable: true });
};
var __copyProps = (to, from, except, desc) => {
  if (from && typeof from === "object" || typeof from === "function") {
    for (let key of __getOwnPropNames(from))
      if (!__hasOwnProp.call(to, key) && key !== except)
        __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
  }
  return to;
};
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
var SwcChooseChildrenDialog_exports = {};
__export(SwcChooseChildrenDialog_exports, {
  SwcChooseChildrenDialog: () => SwcChooseChildrenDialog
});
module.exports = __toCommonJS(SwcChooseChildrenDialog_exports);
var import__ = require("../index");
var import_SwcHTMLDialogElementBase = require("../elements/base/SwcHTMLDialogElementBase");
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);
};
let SwcChooseChildrenDialog = class SwcChooseChildrenDialog2 extends import_SwcHTMLDialogElementBase.SwcHTMLDialogElementBase {
  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([
  (0, import__.attribute)("swc-value"),
  __metadata("design:type", Object)
], SwcChooseChildrenDialog.prototype, "_swcValue", void 0);
__decorate([
  (0, import__.onAttributeChanged)("swc-value"),
  __metadata("design:type", Function),
  __metadata("design:paramtypes", []),
  __metadata("design:returntype", void 0)
], SwcChooseChildrenDialog.prototype, "onValueChange", null);
SwcChooseChildrenDialog = __decorate([
  (0, import__.elementDefine)({ name: "swc-choose-children-dialog", extends: "dialog" })
], SwcChooseChildrenDialog);
//# sourceMappingURL=SwcChooseChildrenDialog.js.map
