"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 WalCommit_exports = {};
__export(WalCommit_exports, {
  MAX_SEGMENT_LINES: () => MAX_SEGMENT_LINES,
  commitIdToNumber: () => commitIdToNumber,
  compareCommitIds: () => compareCommitIds,
  compressedSegmentFilename: () => compressedSegmentFilename,
  decodeContextKey: () => decodeContextKey,
  deserializeCommit: () => deserializeCommit,
  encodeContextKey: () => encodeContextKey,
  isCompressedSegmentFile: () => isCompressedSegmentFile,
  parseSegmentFilename: () => parseSegmentFilename,
  segmentFilename: () => segmentFilename,
  serializeCommit: () => serializeCommit
});
module.exports = __toCommonJS(WalCommit_exports);
var import_StorageDriver = require("../StorageDriver.js");
var import_StringifyTools = require("../StringifyTools.js");
/**
 * @license
 * Copyright 2022-2026 Matter.js Authors
 * SPDX-License-Identifier: Apache-2.0
 */
const MAX_SEGMENT_LINES = 65535;
function encodeContextKey(contexts) {
  return contexts.map((s) => s.replace(/%/g, "%25").replace(/\//g, "%2F")).join("/");
}
function decodeContextKey(key) {
  return key.split("/").map((s) => s.replace(/%2F/gi, "/").replace(/%25/g, "%"));
}
function serializeCommit(commit) {
  return (0, import_StringifyTools.toJson)({ ts: commit.ts, ops: commit.ops });
}
function deserializeCommit(line) {
  const parsed = (0, import_StringifyTools.fromJson)(line);
  if (Array.isArray(parsed)) {
    parsed.forEach(validateOp);
    return { ts: 0, ops: parsed };
  }
  if (!isPlainRecord(parsed)) {
    throw new import_StorageDriver.StorageError("WAL commit is not an object");
  }
  const { ts, ops } = parsed;
  if (typeof ts !== "number") {
    throw new import_StorageDriver.StorageError("WAL commit `ts` is missing or not a number");
  }
  if (!Array.isArray(ops)) {
    throw new import_StorageDriver.StorageError("WAL commit `ops` is missing or not an array");
  }
  ops.forEach(validateOp);
  return { ts, ops };
}
function isPlainRecord(v) {
  return v !== null && typeof v === "object" && !Array.isArray(v);
}
function validateOp(op, index) {
  if (!isPlainRecord(op)) {
    throw new import_StorageDriver.StorageError(`WAL op[${index}] is not an object`);
  }
  const { op: kind, key, values } = op;
  if (typeof key !== "string") {
    throw new import_StorageDriver.StorageError(`WAL op[${index}] \`key\` is not a string`);
  }
  if (kind === "upd") {
    if (!isPlainRecord(values)) {
      throw new import_StorageDriver.StorageError(`WAL op[${index}] upd \`values\` is not an object`);
    }
    return;
  }
  if (kind === "del") {
    if (values === void 0) {
      return;
    }
    if (!Array.isArray(values) || !values.every((v) => typeof v === "string")) {
      throw new import_StorageDriver.StorageError(`WAL op[${index}] del \`values\` is not a string array`);
    }
    return;
  }
  throw new import_StorageDriver.StorageError(`WAL op[${index}] has unknown op kind \`${String(kind)}\``);
}
function segmentFilename(segment) {
  return segment.toString(16).padStart(8, "0") + ".jsonl";
}
function compressedSegmentFilename(segment) {
  return segment.toString(16).padStart(8, "0") + ".jsonl.gz";
}
function isCompressedSegmentFile(name) {
  return /^[0-9a-f]{8}\.jsonl\.gz$/i.test(name);
}
function parseSegmentFilename(name) {
  const match = name.match(/^([0-9a-f]{8})\.jsonl(\.gz)?$/i);
  if (!match) {
    return void 0;
  }
  return parseInt(match[1], 16);
}
function commitIdToNumber(id) {
  return id.segment * 65536 + id.offset;
}
function compareCommitIds(a, b) {
  if (a.segment !== b.segment) {
    return a.segment - b.segment;
  }
  return a.offset - b.offset;
}
//# sourceMappingURL=WalCommit.js.map
