"use strict";
/* Socket Lib - Built with rolldown */
Object.defineProperty(exports, Symbol.toStringTag, { value: 'Module' });
const require_primordials_object = require('../primordials/object.js');
const require_primordials_reflect = require('../primordials/reflect.js');
const require_sorts_natural = require('../sorts/natural.js');

//#region src/objects/sort.ts
/**
* @file Sorted-object helpers: `entryKeyComparator`, `objectEntries`,
*   `toSortedObject`, `toSortedObjectFromEntries`. Symbol keys sort separately
*   from string keys (placed first in the resulting object) — this matters for
*   serialization stability since `Object.keys` and `JSON.stringify` will
*   iterate insertion order.
*/
/**
* Compare two entry arrays by their keys for sorting.
*
* Used internally for alphabetically sorting object entries. String keys are
* compared directly, non-string keys are converted to strings first.
*
* @example
*   ;```ts
*   const entries = [
*     ['zebra', 1],
*     ['apple', 2],
*     ['banana', 3],
*   ]
*   entries.sort(entryKeyComparator)
*   // [['apple', 2], ['banana', 3], ['zebra', 1]]
*   ```
*
* @param a - First entry tuple [key, value]
* @param b - Second entry tuple [key, value]
*
* @returns Negative if a < b, positive if a > b, zero if equal
*/
/*@__NO_SIDE_EFFECTS__*/
function entryKeyComparator(a, b) {
	const keyA = a[0];
	const keyB = b[0];
	return /* @__PURE__ */ require_sorts_natural.localeCompare(typeof keyA === "string" ? keyA : String(keyA), typeof keyB === "string" ? keyB : String(keyB));
}
/**
* Get all own property entries (key-value pairs) from an object.
*
* Unlike `Object.entries()`, this includes non-enumerable properties and symbol
* keys. Returns an empty array for null/undefined.
*
* @example
*   ;```ts
*   objectEntries({ a: 1, b: 2 }) // [['a', 1], ['b', 2]]
*   objectEntries({ [Symbol('k')]: 'v', x: 10 }) // [[Symbol(k), 'v'], ['x', 10]]
*   objectEntries(null) // []
*   ```
*
* @param obj - The object to get entries from.
*
* @returns Array of [key, value] tuples, or empty array for null/undefined
*/
/*@__NO_SIDE_EFFECTS__*/
function objectEntries(obj) {
	if (obj === null || obj === void 0) return [];
	const keys = require_primordials_reflect.ReflectOwnKeys(obj);
	const { length } = keys;
	const entries = Array(length);
	const record = obj;
	for (let i = 0; i < length; i += 1) {
		const key = keys[i];
		entries[i] = [key, record[key]];
	}
	return entries;
}
/**
* Convert an object to a new object with sorted keys.
*
* Creates a new object with the same properties as the input, but with keys
* sorted alphabetically. Symbol keys are sorted separately and placed first.
* This is useful for consistent key ordering in serialization or comparisons.
*
* @example
*   ;```ts
*   toSortedObject({ z: 1, a: 2, m: 3 }) // { a: 2, m: 3, z: 1 }
*   ```
*
* @param obj - The object to sort.
*
* @returns A new object with sorted keys
*/
/*@__NO_SIDE_EFFECTS__*/
function toSortedObject(obj) {
	return /* @__PURE__ */ toSortedObjectFromEntries(/* @__PURE__ */ objectEntries(obj));
}
/**
* Create an object from entries with sorted keys.
*
* Takes an iterable of [key, value] entries and creates a new object with keys
* sorted alphabetically. Symbol keys are sorted separately and placed first in
* the resulting object.
*
* @example
*   ;```ts
*   toSortedObjectFromEntries([
*     ['z', 1],
*     ['a', 2],
*     ['m', 3],
*   ])
*   // { a: 2, m: 3, z: 1 }
*   ```
*
* @param entries - Iterable of [key, value] tuples.
*
* @returns A new object with sorted keys
*/
/*@__NO_SIDE_EFFECTS__*/
function toSortedObjectFromEntries(entries) {
	const otherEntries = [];
	const symbolEntries = [];
	for (const entry of entries) if (typeof entry[0] === "symbol") symbolEntries.push(entry);
	else otherEntries.push(entry);
	if (!otherEntries.length && !symbolEntries.length) return {};
	return require_primordials_object.ObjectFromEntries([...symbolEntries.sort(entryKeyComparator), ...otherEntries.sort(entryKeyComparator)]);
}

//#endregion
exports.entryKeyComparator = entryKeyComparator;
exports.objectEntries = objectEntries;
exports.toSortedObject = toSortedObject;
exports.toSortedObjectFromEntries = toSortedObjectFromEntries;