import { Jt as getNodeSqliteKysely, Kt as executeSqliteQuerySync, s as runOpenClawStateWriteTransaction } from "./openclaw-state-db-Cb4e1jdd.js";
import { i as logWarn } from "./logger-DJxYfLJ7.js";
import { c as openSkillWorkshopStore, o as databaseOptions, r as parseSkillProposalRow, s as ensureSkillWorkshopSchema } from "./store-sqlite-record-ByiMFhcJ.js";
import path from "node:path";
//#region src/skills/workshop/ownership.ts
function setWorkshopOwnershipClaimRelease(database, workspaceDir, skillDirs, releaseTime) {
	const targetDirs = new Set(skillDirs.map((skillDir) => path.resolve(skillDir)));
	if (targetDirs.size === 0) return;
	const kysely = getNodeSqliteKysely(database);
	const proposalIds = executeSqliteQuerySync(database, kysely.selectFrom("skill_workshop_proposals").selectAll().where("workspace_dir", "=", path.resolve(workspaceDir)).where("kind", "=", "create").where("status", "=", "applied")).rows.flatMap((row) => {
		const record = parseSkillProposalRow(row);
		return record && targetDirs.has(path.resolve(record.target.skillDir)) ? [record.id] : [];
	});
	if (proposalIds.length === 0) return;
	executeSqliteQuerySync(database, kysely.updateTable("skill_workshop_proposals").set({ claim_released_time: releaseTime }).where("proposal_id", "in", proposalIds));
}
function writeWorkshopOwnershipClaims(workspaceDir, claimedSkillDirs, releasedSkillDirs, releaseTime, options) {
	const claimedDirs = new Set(claimedSkillDirs.map((skillDir) => path.resolve(skillDir)));
	const releasedDirs = releasedSkillDirs.filter((skillDir) => !claimedDirs.has(path.resolve(skillDir)));
	if (claimedDirs.size === 0 && releasedDirs.length === 0) return;
	ensureSkillWorkshopSchema(options);
	runOpenClawStateWriteTransaction(({ db }) => {
		setWorkshopOwnershipClaimRelease(db, workspaceDir, releasedDirs, releaseTime);
		setWorkshopOwnershipClaimRelease(db, workspaceDir, [...claimedDirs], null);
	}, databaseOptions(options));
}
function releaseWorkshopOwnershipClaims(workspaceDir, skillDirs, releaseTime, options = {}) {
	writeWorkshopOwnershipClaims(workspaceDir, [], skillDirs, releaseTime, options);
}
function restoreWorkshopOwnershipClaims(workspaceDir, skillDirs, resultSkillDirs, releaseTime, options = {}) {
	writeWorkshopOwnershipClaims(workspaceDir, skillDirs, resultSkillDirs, releaseTime, options);
}
function restoreWorkshopOwnershipClaimsBestEffort(workspaceDir, skillDirs, options = {}) {
	try {
		writeWorkshopOwnershipClaims(workspaceDir, skillDirs, [], 0, options);
	} catch (error) {
		logWarn(`skill-workshop: failed to reclaim ownership after rollback: ${String(error)}`);
	}
}
/** Paths claimed by a successfully applied Workshop create proposal. */
function listWorkshopOwnedSkillDirs(workspaceDir, options = {}) {
	const { database, kysely } = openSkillWorkshopStore(options);
	const rows = executeSqliteQuerySync(database.db, kysely.selectFrom("skill_workshop_proposals").selectAll().where("workspace_dir", "=", path.resolve(workspaceDir)).where("kind", "=", "create").where("status", "=", "applied").where("claim_released_time", "is", null)).rows;
	return new Set(rows.flatMap((row) => {
		const record = parseSkillProposalRow(row);
		return record ? [path.resolve(record.target.skillDir)] : [];
	}));
}
function isWorkshopOwnedSkillDir(workspaceDir, skillDir, options = {}) {
	return listWorkshopOwnedSkillDirs(workspaceDir, options).has(path.resolve(skillDir));
}
//#endregion
export { restoreWorkshopOwnershipClaimsBestEffort as a, restoreWorkshopOwnershipClaims as i, listWorkshopOwnedSkillDirs as n, releaseWorkshopOwnershipClaims as r, isWorkshopOwnedSkillDir as t };
