import { r as __exportAll } from "./chunk-BSw8zbkd.mjs";
import { D as object, O as string, S as array, T as literal, _ as generateId, a as BaseDocumentHeaderSchema, c as baseCreateDocument, f as createAction, g as defaultBaseState, h as createState, l as baseLoadFromInput, m as createReducer, o as BaseDocumentStateSchema, s as baseActions, u as baseSaveToFileHandle, v as isDocumentAction, x as any } from "./dist-JCXjvFc8.mjs";
//#region document-models/group/v1/gen/document-model.ts
const documentModel = {
	id: "powerchat/group",
	name: "Group",
	author: {
		name: "Powerhouse",
		website: "https://www.powerhouse.inc/"
	},
	extension: "pgrp",
	description: "A channel group that organizes and orders channels in the PowerChat sidebar.",
	specifications: [{
		state: {
			local: {
				schema: "",
				examples: [],
				initialValue: ""
			},
			global: {
				schema: "type GroupState {\n  name: String\n  channelIds: [PHID!]!\n}",
				examples: [],
				initialValue: "{\"name\": null, \"channelIds\": []}"
			}
		},
		modules: [{
			id: "mod-group",
			name: "group",
			description: "Group management operations",
			operations: [
				{
					id: "op-set-group-name",
					name: "SET_GROUP_NAME",
					description: "Sets the group name",
					schema: "input SetGroupNameInput {\n  name: String!\n}",
					template: "Sets the group name",
					reducer: "state.name = action.input.name;",
					errors: [],
					examples: [],
					scope: "global"
				},
				{
					id: "op-add-channel",
					name: "ADD_CHANNEL",
					description: "Adds a channel to this group",
					schema: "input AddChannelInput {\n  channelId: PHID!\n}",
					template: "Adds a channel to this group",
					reducer: "if (state.channelIds.includes(action.input.channelId)) {\n  throw new ChannelAlreadyInGroupError(`Channel ${action.input.channelId} already in group`);\n}\nstate.channelIds.push(action.input.channelId);",
					errors: [{
						id: "err-channel-already",
						name: "ChannelAlreadyInGroupError",
						code: "CHANNEL_ALREADY_IN_GROUP",
						description: "Channel is already in this group",
						template: ""
					}],
					examples: [],
					scope: "global"
				},
				{
					id: "op-remove-channel",
					name: "REMOVE_CHANNEL",
					description: "Removes a channel from this group",
					schema: "input RemoveChannelInput {\n  channelId: PHID!\n}",
					template: "Removes a channel from this group",
					reducer: "const index = state.channelIds.indexOf(action.input.channelId);\nif (index === -1) {\n  throw new ChannelNotInGroupError(`Channel ${action.input.channelId} not in group`);\n}\nstate.channelIds.splice(index, 1);",
					errors: [{
						id: "err-channel-not-in",
						name: "ChannelNotInGroupError",
						code: "CHANNEL_NOT_IN_GROUP",
						description: "Channel is not in this group",
						template: ""
					}],
					examples: [],
					scope: "global"
				},
				{
					id: "op-reorder-channels",
					name: "REORDER_CHANNELS",
					description: "Reorders channels within the group",
					schema: "input ReorderChannelsInput {\n  channelIds: [PHID!]!\n}",
					template: "Reorders channels within the group",
					reducer: "state.channelIds = action.input.channelIds;",
					errors: [],
					examples: [],
					scope: "global"
				}
			]
		}],
		version: 1,
		changeLog: []
	}]
};
//#endregion
//#region document-models/group/v1/gen/group/error.ts
var ChannelAlreadyInGroupError = class extends Error {
	errorCode = "ChannelAlreadyInGroupError";
	constructor(message = "ChannelAlreadyInGroupError") {
		super(message);
	}
};
var ChannelNotInGroupError = class extends Error {
	errorCode = "ChannelNotInGroupError";
	constructor(message = "ChannelNotInGroupError") {
		super(message);
	}
};
//#endregion
//#region document-models/group/v1/src/reducers/group.ts
const groupGroupOperations = {
	setGroupNameOperation(state, action) {
		state.name = action.input.name;
	},
	addChannelOperation(state, action) {
		if (state.channelIds.includes(action.input.channelId)) throw new ChannelAlreadyInGroupError(`Channel ${action.input.channelId} already in group`);
		state.channelIds.push(action.input.channelId);
	},
	removeChannelOperation(state, action) {
		const index = state.channelIds.indexOf(action.input.channelId);
		if (index === -1) throw new ChannelNotInGroupError(`Channel ${action.input.channelId} not in group`);
		state.channelIds.splice(index, 1);
	},
	reorderChannelsOperation(state, action) {
		state.channelIds = action.input.channelIds;
	}
};
//#endregion
//#region document-models/group/v1/gen/schema/zod.ts
const isDefinedNonNullAny = (v) => v !== void 0 && v !== null;
const definedNonNullAnySchema = any().refine((v) => isDefinedNonNullAny(v));
function AddChannelInputSchema() {
	return object({ channelId: string() });
}
function GroupStateSchema() {
	return object({
		__typename: literal("GroupState").optional(),
		channelIds: array(string()),
		name: string().nullish()
	});
}
function RemoveChannelInputSchema() {
	return object({ channelId: string() });
}
function ReorderChannelsInputSchema() {
	return object({ channelIds: array(string()) });
}
function SetGroupNameInputSchema() {
	return object({ name: string() });
}
//#endregion
//#region document-models/group/v1/gen/reducer.ts
const stateReducer = (state, action, dispatch) => {
	if (isDocumentAction(action)) return state;
	switch (action.type) {
		case "SET_GROUP_NAME":
			SetGroupNameInputSchema().parse(action.input);
			groupGroupOperations.setGroupNameOperation(state[action.scope], action, dispatch);
			break;
		case "ADD_CHANNEL":
			AddChannelInputSchema().parse(action.input);
			groupGroupOperations.addChannelOperation(state[action.scope], action, dispatch);
			break;
		case "REMOVE_CHANNEL":
			RemoveChannelInputSchema().parse(action.input);
			groupGroupOperations.removeChannelOperation(state[action.scope], action, dispatch);
			break;
		case "REORDER_CHANNELS":
			ReorderChannelsInputSchema().parse(action.input);
			groupGroupOperations.reorderChannelsOperation(state[action.scope], action, dispatch);
			break;
		default: return state;
	}
};
const reducer = createReducer(stateReducer);
//#endregion
//#region document-models/group/v1/gen/group/creators.ts
var creators_exports = /* @__PURE__ */ __exportAll({
	addChannel: () => addChannel,
	removeChannel: () => removeChannel,
	reorderChannels: () => reorderChannels,
	setGroupName: () => setGroupName
});
const setGroupName = (input) => createAction("SET_GROUP_NAME", { ...input }, void 0, SetGroupNameInputSchema, "global");
const addChannel = (input) => createAction("ADD_CHANNEL", { ...input }, void 0, AddChannelInputSchema, "global");
const removeChannel = (input) => createAction("REMOVE_CHANNEL", { ...input }, void 0, RemoveChannelInputSchema, "global");
const reorderChannels = (input) => createAction("REORDER_CHANNELS", { ...input }, void 0, ReorderChannelsInputSchema, "global");
//#endregion
//#region document-models/group/v1/actions.ts
/** Actions for the Group document model */
const actions = {
	...baseActions,
	...creators_exports
};
//#endregion
//#region document-models/group/v1/gen/document-type.ts
const groupDocumentType = "powerchat/group";
//#endregion
//#region document-models/group/v1/gen/document-schema.ts
/** Schema for validating the header object of a Group document */
const GroupDocumentHeaderSchema = BaseDocumentHeaderSchema.extend({ documentType: literal(groupDocumentType) });
/** Schema for validating the state object of a Group document */
const GroupPHStateSchema = BaseDocumentStateSchema.extend({ global: GroupStateSchema() });
const GroupDocumentSchema = object({
	header: GroupDocumentHeaderSchema,
	state: GroupPHStateSchema,
	initialState: GroupPHStateSchema
});
/** Simple helper function to check if a state object is a Group document state object */
function isGroupState(state) {
	return GroupPHStateSchema.safeParse(state).success;
}
/** Simple helper function to assert that a document state object is a Group document state object */
function assertIsGroupState(state) {
	GroupPHStateSchema.parse(state);
}
/** Simple helper function to check if a document is a Group document */
function isGroupDocument(document) {
	return GroupDocumentSchema.safeParse(document).success;
}
/** Simple helper function to assert that a document is a Group document */
function assertIsGroupDocument(document) {
	GroupDocumentSchema.parse(document);
}
//#endregion
//#region document-models/group/v1/gen/utils.ts
const initialGlobalState = {
	name: null,
	channelIds: []
};
const initialLocalState = {};
const utils$1 = {
	fileExtension: "pgrp",
	createState(state) {
		return {
			...defaultBaseState(),
			global: {
				...initialGlobalState,
				...state?.global
			},
			local: {
				...initialLocalState,
				...state?.local
			}
		};
	},
	createDocument(state) {
		const document = baseCreateDocument(utils$1.createState, state);
		document.header.documentType = groupDocumentType;
		document.header.id = generateId();
		return document;
	},
	saveToFileHandle(document, input) {
		return baseSaveToFileHandle(document, input);
	},
	loadFromInput(input) {
		return baseLoadFromInput(input, reducer);
	},
	isStateOfType(state) {
		return isGroupState(state);
	},
	assertIsStateOfType(state) {
		return assertIsGroupState(state);
	},
	isDocumentOfType(document) {
		return isGroupDocument(document);
	},
	assertIsDocumentOfType(document) {
		return assertIsGroupDocument(document);
	}
};
//#endregion
//#region document-models/group/v1/src/utils.ts
var utils_exports = /* @__PURE__ */ __exportAll({});
//#endregion
//#region document-models/group/v1/utils.ts
/** Utils for the Group document model */
const utils = {
	...utils$1,
	...utils_exports
};
//#endregion
//#region document-models/group/v1/module.ts
/** Document model module for the Group document type */
const Group = {
	version: 1,
	reducer,
	actions,
	utils,
	documentModel: createState(defaultBaseState(), documentModel)
};
//#endregion
export { RemoveChannelInputSchema as C, isDefinedNonNullAny as D, definedNonNullAnySchema as E, documentModel as O, GroupStateSchema as S, SetGroupNameInputSchema as T, removeChannel as _, utils$1 as a, reducer as b, GroupPHStateSchema as c, isGroupDocument as d, isGroupState as f, creators_exports as g, addChannel as h, initialLocalState as i, assertIsGroupDocument as l, actions as m, utils as n, GroupDocumentHeaderSchema as o, groupDocumentType as p, initialGlobalState as r, GroupDocumentSchema as s, Group as t, assertIsGroupState as u, reorderChannels as v, ReorderChannelsInputSchema as w, AddChannelInputSchema as x, setGroupName as y };

//# sourceMappingURL=module-Du2F4HUY.mjs.map