/**
 * @license
 * Copyright 2022-2026 Matter.js Authors
 * SPDX-License-Identifier: Apache-2.0
 */
import { isDeepEqual } from "@matter/general";
class ApiPath {
  #segments = Array();
  /**
   * Create a new path from:
   *
   * * An {@link AppAddress#pathname}
   * * A text path, delimited with "/" with URL encoded segments
   * * An array of decoded segments
   *
   * Ignores path segments that are empty or ".".  ".." resolves up one level.
   *
   * So generally normal UNIX/URL semantics.
   */
  constructor(path) {
    if (path instanceof URL) {
      path = path.pathname;
    }
    if (!Array.isArray(path)) {
      path = path.split("/").map(decodeURIComponent);
    }
    for (const segment of path) {
      if (segment === "" || segment === ".") {
        continue;
      }
      if (segment === "..") {
        this.#segments.pop();
        continue;
      }
      this.#segments.push(segment);
    }
  }
  get isEmpty() {
    return !this.#segments.length;
  }
  [Symbol.iterator]() {
    return this.#segments[Symbol.iterator]();
  }
  slice(start, end) {
    return new ApiPath(this.#segments.slice(start, end));
  }
  at(path) {
    if (this.isEmpty) {
      return new ApiPath(path);
    }
    if (path instanceof URL) {
      path = path.pathname;
    }
    if (Array.isArray(path)) {
      path = path.map(encodeURIComponent).join("/");
    }
    if (path.startsWith("/")) {
      return new ApiPath(path);
    }
    return new ApiPath(`${this.toString()}/${path}`);
  }
  toString() {
    return `${this.#segments.map(encodeURIComponent).join("/")}`;
  }
  includes(other) {
    return isDeepEqual(this.#segments, other.slice(0, this.#segments.length).#segments);
  }
  subpathFor(other) {
    if (!this.#segments.length) {
      return other;
    }
    if (!this.includes(other)) {
      return void 0;
    }
    return other.slice(this.#segments.length);
  }
}
export {
  ApiPath
};
//# sourceMappingURL=ApiPath.js.map
