import { basename } from "node:path";
import { Paths } from "../../const.js";
import { matchMultiplePaths, matchPath, renderPath, } from "../../transform/shared.js";
import { sendChangeInfoChannel } from "./peer.js";
export const DependencyInfoPath = "package.json/:packageName";
export const sendChangeInfoHelper = (clark, chatSessionStore) => {
    const apiPathMatcher = matchPath(Paths.GeneratedApis);
    const componentPathMatcher = matchMultiplePaths(Paths.ComponentsFolder, Paths.ComponentsFile);
    const pagePathMatcher = matchPath(Paths.PageIndexes);
    const dependencyPathMatcher = matchPath(DependencyInfoPath);
    const sendChangeInfo = sendChangeInfoChannel(clark, chatSessionStore);
    const push = (info) => {
        clark.updateContext((context) => ({
            ...context,
            generatedChangeInfo: {
                ...context.generatedChangeInfo,
                [info.filePath]: info,
            },
        }));
        return sendChangeInfo(info);
    };
    if (!clark.context.generatedChangeInfo) {
        clark.updateContext({
            generatedChangeInfo: {},
        });
    }
    return ({ type, filePath, action, }) => {
        const { initialFilePaths } = clark.context;
        const existingChangeInfo = clark.context.generatedChangeInfo?.[filePath];
        switch (type) {
            case "ready":
            case "validated":
            case "error": {
                if (!existingChangeInfo) {
                    return;
                }
                return push({
                    ...existingChangeInfo,
                    status: type,
                });
            }
            case "pending": {
                const { apis, components } = initialFilePaths ?? {};
                if (existingChangeInfo) {
                    return push({
                        ...existingChangeInfo,
                        status: "pending",
                    });
                }
                const dependencyPath = dependencyPathMatcher(filePath);
                if (dependencyPath.matched) {
                    const { packageName } = dependencyPath.params;
                    return push({
                        type: "dependency",
                        name: packageName,
                        action: action ?? "create",
                        status: "pending",
                        filePath,
                    });
                }
                const componentPath = componentPathMatcher(filePath);
                if (componentPath.matched) {
                    let { componentName } = componentPath.params;
                    componentName = basename(componentName, ".tsx");
                    return push({
                        type: "component",
                        name: componentName,
                        action: components?.includes(componentName) ? "edit" : "create",
                        status: "pending",
                        filePath,
                    });
                }
                const pagePath = pagePathMatcher(filePath);
                if (pagePath.matched) {
                    const { pageName } = pagePath.params;
                    return push({
                        type: "page",
                        name: pageName,
                        action: "edit",
                        status: "pending",
                        filePath,
                    });
                }
                if (filePath === Paths.AppTheme) {
                    return push({
                        type: "theme",
                        name: "App Theme",
                        action: "edit",
                        status: "pending",
                        filePath,
                    });
                }
                const apiPath = apiPathMatcher(filePath);
                if (apiPath.matched) {
                    let { apiName } = apiPath.params;
                    apiName = basename(apiName, ".ts");
                    const yamlPath = renderPath(Paths.ApiYamls, {
                        ...apiPath.params,
                        apiName,
                    });
                    return push({
                        type: "api",
                        name: apiName,
                        action: apis?.includes(yamlPath) ? "edit" : "create",
                        status: "pending",
                        filePath,
                    });
                }
            }
        }
    };
};
//# sourceMappingURL=change-info.js.map