"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 ApiPath_exports = {};
__export(ApiPath_exports, {
  ApiPath: () => ApiPath
});
module.exports = __toCommonJS(ApiPath_exports);
var import_general = require("#general");
/**
 * @license
 * Copyright 2022-2026 Matter.js Authors
 * SPDX-License-Identifier: Apache-2.0
 */
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 (0, import_general.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);
  }
}
//# sourceMappingURL=ApiPath.js.map
