"use strict";
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 FileDesignator_exports = {};
__export(FileDesignator_exports, {
  FileDesignator: () => FileDesignator
});
module.exports = __toCommonJS(FileDesignator_exports);
var import_general = require("@matter/general");
var import_types = require("@matter/types");
/**
 * @license
 * Copyright 2022-2026 Matter.js Authors
 * SPDX-License-Identifier: Apache-2.0
 */
class FileDesignator {
  #fd;
  /**
   * Initialize a FileDesignator from a BDX URI string. Also returns the source node ID of the owner of the file.
   */
  static fromBdxUri(uri) {
    const bdxParts = uri.match(/^bdx:\/\/([0-9A-F]{16})\/(\S+)$/);
    if (!bdxParts) {
      throw new import_general.ImplementationError(`Invalid OTA URI "${uri}"`);
    }
    return {
      fileDesignator: new FileDesignator(bdxParts[2]),
      sourceNodeId: (0, import_types.NodeId)(BigInt(`0x${bdxParts[1]}`))
    };
  }
  /** Create a FileDesignator from a string or bytes object. */
  constructor(fd) {
    if (typeof fd === "string") {
      this.#fd = import_general.Bytes.fromString(fd);
    } else {
      this.#fd = fd;
    }
  }
  /** Return the bytes representation of the FileDesignator. */
  get bytes() {
    return this.#fd;
  }
  /** Return the textual/string representation of the FileDesignator. */
  get text() {
    const fileDesignatorData = import_general.Bytes.of(this.#fd);
    const isValidName = fileDesignatorData.every(
      (byte) => byte >= 65 && byte <= 90 || // A-Z
      byte >= 97 && byte <= 122 || // a-z
      byte >= 48 && byte <= 57 || // 0..9
      byte === 46 || // "."
      byte === 45 || // "-"
      byte === 95 || // "_"
      byte === 47
      // "/"
    );
    if (isValidName) {
      return fileDesignatorData.reduce((name, byte) => name + String.fromCharCode(byte), "");
    } else {
      return `0x${import_general.Bytes.toHex(fileDesignatorData)}`;
    }
  }
  /**
   * Return the FileDesignator as a BDX URI string. The source node ID of the owner of the file is required to
   * generate a valid URI.
   */
  asBdxUri(sourceNode) {
    return `bdx://${import_types.NodeId.strOf(sourceNode).toUpperCase()}/${this.text}`;
  }
  get [import_general.Diagnostic.value]() {
    return this.text;
  }
}
//# sourceMappingURL=FileDesignator.js.map
