import { findAllMidsceneLocatorField } from "@godscene/core/ai-model";
class BasePlaygroundAdapter {
    async getActionSpace(_context) {
        return [];
    }
    validateParams(value, action) {
        if (!action?.paramSchema) return {
            valid: true
        };
        const needsStructuredParams = this.actionNeedsStructuredParams(action);
        if (!needsStructuredParams) return {
            valid: true
        };
        if (!value.params) return {
            valid: false,
            errorMessage: 'Parameters are required'
        };
        try {
            const paramsForValidation = this.prepareParamsForValidation(value.params, action);
            action.paramSchema.parse(paramsForValidation);
            return {
                valid: true
            };
        } catch (error) {
            return this.handleValidationError(error);
        }
    }
    createDisplayContent(value, needsStructuredParams, action) {
        if (!needsStructuredParams || !value.params || !action?.paramSchema) return value.prompt || '';
        const paramsList = this.buildParamsDisplayList(value.params, action);
        return paramsList.join('\n') || value.prompt || '';
    }
    formatBasicErrorMessage(error) {
        return error?.message || 'Unknown error';
    }
    getSchemaKeys(action) {
        if (!action?.paramSchema || !('shape' in action.paramSchema)) return [];
        const schema = action.paramSchema;
        return schema && 'shape' in schema ? Object.keys(schema.shape) : [];
    }
    filterValidParams(params, excludeKeys = []) {
        const filtered = {};
        Object.keys(params).forEach((key)=>{
            if (!excludeKeys.includes(key) && void 0 !== params[key] && null !== params[key] && '' !== params[key]) filtered[key] = params[key];
        });
        return filtered;
    }
    actionNeedsStructuredParams(action) {
        if ('object' == typeof action.paramSchema && 'shape' in action.paramSchema) {
            const shape = action.paramSchema.shape || {};
            return Object.keys(shape).length > 0;
        }
        return true;
    }
    prepareParamsForValidation(params, action) {
        const paramsForValidation = {
            ...params
        };
        if (action.paramSchema) {
            const locatorFieldKeys = findAllMidsceneLocatorField(action.paramSchema);
            locatorFieldKeys.forEach((key)=>{
                if ('string' == typeof paramsForValidation[key]) paramsForValidation[key] = {
                    midscene_location_field_flag: true,
                    prompt: paramsForValidation[key],
                    center: [
                        0,
                        0
                    ],
                    rect: {
                        left: 0,
                        top: 0,
                        width: 0,
                        height: 0
                    }
                };
            });
        }
        return paramsForValidation;
    }
    handleValidationError(error) {
        const zodError = error;
        if (zodError.errors && zodError.errors.length > 0) {
            const errorMessages = zodError.errors.filter((err)=>{
                const path = err.path.join('.');
                return !path.includes('center') && !path.includes('rect') && !path.includes('midscene_location_field_flag');
            }).map((err)=>{
                const field = err.path.join('.');
                return `${field}: ${err.message}`;
            });
            if (errorMessages.length > 0) return {
                valid: false,
                errorMessage: `Validation error: ${errorMessages.join(', ')}`
            };
        }
        const errorMsg = error instanceof Error ? error.message : 'Unknown validation error';
        return {
            valid: false,
            errorMessage: `Parameter validation failed: ${errorMsg}`
        };
    }
    buildParamsDisplayList(params, action) {
        const paramsList = [];
        const schema = action.paramSchema;
        if (!(schema && 'shape' in schema)) return paramsList;
        const locatorFieldKeys = findAllMidsceneLocatorField(schema);
        const shapeKeys = Object.keys(schema.shape);
        shapeKeys.forEach((key)=>{
            const paramValue = params[key];
            if (this.isValidParamValue(paramValue)) {
                const displayKey = this.capitalizeFirstLetter(key);
                const formattedValue = this.formatParamValue(key, paramValue, locatorFieldKeys.includes(key));
                paramsList.push(`${displayKey}: ${formattedValue}`);
            }
        });
        return paramsList;
    }
    isValidParamValue(value) {
        return null != value && '' !== value;
    }
    capitalizeFirstLetter(str) {
        return str.charAt(0).toUpperCase() + str.slice(1);
    }
    formatParamValue(key, value, isLocateField) {
        if (isLocateField || 'string' == typeof value) return `"${value}"`;
        if ('number' == typeof value) return 'distance' === key ? `${value}px` : `${value}`;
        return `${value}`;
    }
}
export { BasePlaygroundAdapter };
