import { cleanBlocksForDescendant } from "./docx-table-ops.js";
//#region extensions/feishu/src/tools/docx/docx-batch-insert.ts
const BATCH_SIZE = 1e3;
function readStringValue(value) {
	return typeof value === "string" && value.trim() ? value : void 0;
}
function normalizeChildIds(children) {
	if (Array.isArray(children)) return children;
	const child = readStringValue(children);
	return child ? [child] : void 0;
}
function toDescendantBlock(block) {
	const children = normalizeChildIds(block.children);
	return {
		...block,
		...children ? { children } : {}
	};
}
function collectDescendants(blockMap, rootId) {
	const result = [];
	const visited = /* @__PURE__ */ new Set();
	function collect(blockId) {
		if (visited.has(blockId)) return;
		visited.add(blockId);
		const block = blockMap.get(blockId);
		if (!block) return;
		result.push(block);
		const children = block.children;
		if (Array.isArray(children)) for (const childId of children) collect(childId);
		else if (typeof children === "string") collect(children);
	}
	collect(rootId);
	return result;
}
async function insertBatch(client, docToken, blocks, firstLevelBlockIds, parentBlockId = docToken, index = -1) {
	const descendants = cleanBlocksForDescendant(blocks);
	if (descendants.length === 0) return [];
	const res = await client.docx.documentBlockDescendant.create({
		path: {
			document_id: docToken,
			block_id: parentBlockId
		},
		data: {
			children_id: firstLevelBlockIds,
			descendants: descendants.map(toDescendantBlock),
			index
		}
	});
	if (res.code !== 0) throw new Error(`${res.msg} (code: ${res.code})`);
	return res.data?.children ?? [];
}
async function insertBlocksInBatches(client, docToken, blocks, firstLevelBlockIds, logger, parentBlockId = docToken, startIndex = -1) {
	const allChildren = [];
	const batches = [];
	let currentBatch = {
		firstLevelIds: [],
		blocks: []
	};
	const usedBlockIds = /* @__PURE__ */ new Set();
	const blockMap = /* @__PURE__ */ new Map();
	for (const block of blocks) if (block.block_id) blockMap.set(block.block_id, block);
	for (const firstLevelId of firstLevelBlockIds) {
		const newBlocks = collectDescendants(blockMap, firstLevelId).filter((b) => b.block_id && !usedBlockIds.has(b.block_id));
		if (newBlocks.length > 1e3) throw new Error(`Block "${firstLevelId}" has ${newBlocks.length} descendants, exceeds Feishu API limit ${BATCH_SIZE}. Split content.`);
		if (currentBatch.blocks.length + newBlocks.length > 1e3 && currentBatch.blocks.length > 0) {
			batches.push(currentBatch);
			currentBatch = {
				firstLevelIds: [],
				blocks: []
			};
		}
		currentBatch.firstLevelIds.push(firstLevelId);
		for (const block of newBlocks) {
			currentBatch.blocks.push(block);
			if (block.block_id) usedBlockIds.add(block.block_id);
		}
	}
	if (currentBatch.blocks.length > 0) batches.push(currentBatch);
	let currentIndex = startIndex;
	for (let i = 0; i < batches.length; i++) {
		const batch = batches[i];
		logger?.info?.(`feishu_doc: Inserting batch ${i + 1}/${batches.length} (${batch.blocks.length} blocks)...`);
		const children = await insertBatch(client, docToken, batch.blocks, batch.firstLevelIds, parentBlockId, currentIndex);
		allChildren.push(...children);
		if (currentIndex !== -1) currentIndex += batch.firstLevelIds.length;
	}
	return {
		children: allChildren,
		skipped: []
	};
}
//#endregion
export { BATCH_SIZE, insertBlocksInBatches };

//# sourceMappingURL=docx-batch-insert.js.map