'use strict';

Object.defineProperty(exports, '__esModule', { value: true });

class BindGroup {
  constructor(resources) {
    this.resources = /* @__PURE__ */ Object.create(null);
    this._dirty = true;
    let index = 0;
    for (const i in resources) {
      const resource = resources[i];
      this.setResource(resource, index++);
    }
    this.updateKey();
  }
  update() {
    this.updateKey();
  }
  updateKey() {
    if (!this._dirty)
      return;
    this._dirty = false;
    const keyParts = [];
    let index = 0;
    for (const i in this.resources) {
      keyParts[index++] = this.resources[i].resourceId;
    }
    this.key = keyParts.join("|");
  }
  setResource(resource, index) {
    const currentResource = this.resources[index];
    if (resource === currentResource)
      return;
    if (currentResource) {
      resource.off?.("change", this.onResourceChange, this);
    }
    resource.on?.("change", this.onResourceChange, this);
    this.resources[index] = resource;
    this._dirty = true;
  }
  getResource(index) {
    return this.resources[index];
  }
  touch(tick) {
    const resources = this.resources;
    for (const i in resources) {
      resources[i].touched = tick;
    }
  }
  destroy() {
    const resources = this.resources;
    for (const i in resources) {
      const resource = resources[i];
      resource.off?.("change", this.onResourceChange, this);
    }
    this.resources = null;
  }
  onResourceChange() {
    this._dirty = true;
    this.update();
  }
}

exports.BindGroup = BindGroup;
//# sourceMappingURL=BindGroup.js.map
