/**
 * @license
 * Copyright 2022-2025 Matter.js Authors
 * SPDX-License-Identifier: Apache-2.0
 */
import { Aspect } from "./Aspect.js";
class Quality extends Aspect {
  /**
   * Initialize from a Quality.All definition or a string conforming to the
   * "other quality" DSL defined in the Matter specification.
   */
  constructor(definition) {
    super(definition);
    if (typeof definition === "string") {
      this.parse(this, definition);
    } else if (Array.isArray(definition)) {
      definition.map((f) => this.parse(this, f));
    } else {
      Object.assign(this, definition);
    }
    this.freeze();
  }
  parse(quality, definition) {
    const text = definition.toUpperCase();
    if (text === "DERIVED") {
      return;
    }
    let disallow = false;
    for (const char of text) {
      if (char === " " || char === "	") {
        continue;
      }
      if (char === "!") {
        disallow = true;
        continue;
      }
      const field = Quality.Flag[char];
      if (field) {
        if (this.disallowed?.[field]) {
          continue;
        }
        if (disallow) {
          delete this[field];
          if (!this.disallowed) {
            this.disallowed = {};
          }
          this.disallowed[field] = true;
          disallow = false;
        } else {
          this[field] = true;
        }
      } else {
        quality.error("UNKNOWN_QUALITY_FLAG", `Unknown flag "${char}"`);
      }
    }
  }
  /**
   * Display quality using standard Matter syntax.
   */
  toString() {
    const flags = [];
    for (const f of Quality.FlagNames) {
      const field = Quality.Flag[f];
      if (this[field] && !this.disallowed?.[field]) {
        flags.push(f);
      }
    }
    return flags.join(" ");
  }
}
((Quality2) => {
  let Field;
  ((Field2) => {
    Field2["nullable"] = "X";
    Field2["nonvolatile"] = "N";
    Field2["fixed"] = "F";
    Field2["scene"] = "S";
    Field2["reportable"] = "P";
    Field2["changesOmitted"] = "C";
    Field2["singleton"] = "I";
    Field2["quieter"] = "Q";
    Field2["largeMessage"] = "L";
    Field2["diagnostics"] = "K";
  })(Field = Quality2.Field || (Quality2.Field = {}));
  let Flag;
  ((Flag2) => {
    Flag2["X"] = "nullable";
    Flag2["N"] = "nonvolatile";
    Flag2["F"] = "fixed";
    Flag2["S"] = "scene";
    Flag2["P"] = "reportable";
    Flag2["C"] = "changesOmitted";
    Flag2["I"] = "singleton";
    Flag2["Q"] = "quieter";
    Flag2["L"] = "largeMessage";
    Flag2["K"] = "diagnostics";
  })(Flag = Quality2.Flag || (Quality2.Flag = {}));
  Quality2.FlagNames = ["X", "N", "F", "S", "P", "C", "I", "Q", "L", "K"];
})(Quality || (Quality = {}));
export {
  Quality
};
//# sourceMappingURL=Quality.js.map
