"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 file_exports = {};
__export(file_exports, {
  PicsFile: () => PicsFile
});
module.exports = __toCommonJS(file_exports);
/**
 * @license
 * Copyright 2022-2026 Matter.js Authors
 * SPDX-License-Identifier: Apache-2.0
 */
class PicsFile {
  #lines;
  #values;
  constructor(lines) {
    if (typeof lines === "string") {
      this.#lines = lines.split("\n").map((l) => l.trim());
    } else {
      this.#lines = lines ?? [];
    }
  }
  get lines() {
    return this.#lines;
  }
  get values() {
    if (!this.#values) {
      const values = {};
      for (const line of this.lines) {
        parseLine(line, values);
      }
      this.#values = values;
    }
    return this.#values;
  }
  toString() {
    return this.#lines.join("\n") + "\n";
  }
  patch(other) {
    const newValues = { ...other.values };
    const newLines = new Array();
    for (const line of this.lines) {
      const lineValues = {};
      if (!parseLine(line, lineValues)) {
        newLines.push(line);
        continue;
      }
      for (const key in lineValues) {
        const newValue = newValues[key];
        if (newValue !== void 0) {
          newLines.push(`${key}=${newValue}`);
          delete newValues[key];
        } else {
          newLines.push(`${key}=${lineValues[key]}`);
        }
      }
    }
    for (const key in newValues) {
      newLines.push(`${key}=${newValues[key]}`);
    }
    this.#values = void 0;
    this.#lines = newLines;
  }
}
function parseLine(line, values) {
  line = line.trim();
  if (line.startsWith("#")) {
    return false;
  }
  const valueMatch = line.match(/^([^=\s]+)=(.*)$/);
  if (!valueMatch) {
    return false;
  }
  const [, key, valueStr] = valueMatch;
  let value;
  switch (valueStr) {
    case "0":
      value = 0;
      break;
    case "1":
      value = 1;
      break;
    default:
      return false;
  }
  const dotdot = key.indexOf("..");
  if (dotdot === -1) {
    values[key] = value;
    return true;
  }
  const base = key.slice(0, dotdot);
  const rest = key.slice(dotdot + 2);
  const suffixDot = rest.indexOf(".");
  const rangeTo = suffixDot === -1 ? rest : rest.slice(0, suffixDot);
  const suffix = suffixDot === -1 ? "" : rest.slice(suffixDot);
  const rangePrefix = base.slice(0, base.length - rangeTo.length);
  const rangeFrom = base.slice(rangePrefix.length);
  const rangeSuffix = suffix;
  const rangeFromNum = Number.parseInt(rangeFrom, 16);
  if (!Number.isFinite(rangeFromNum)) {
    throw new Error(`Invalid PICS: Range start in ${key} is invalid`);
  }
  const rangeToNum = Number.parseInt(rangeTo, 16);
  if (!Number.isFinite(rangeToNum)) {
    throw new Error(`Invalid PICS: Range end in ${key} is invalid`);
  }
  if (rangeToNum < rangeFromNum) {
    throw new Error(`Invalid PICS: Range end ${rangeToNum} is greater than range start ${rangeFromNum}`);
  }
  for (let i = rangeFromNum; i <= rangeToNum; i++) {
    values[rangePrefix + i.toString(16).padStart(rangeTo.length, "0") + rangeSuffix] = value;
  }
  return true;
}
//# sourceMappingURL=file.js.map
