import { z } from 'zod';
import { simctl } from '../executor/simctl.js';
import { resolveDevice } from '../utils/device-resolver.js';
import type { PrivacyAction, PrivacyService } from '../types/simctl.js';
const privacyServices = [
'all',
'calendar',
'contacts-limited',
'contacts',
'location',
'location-always',
'photos-add',
'photos',
'media-library',
'microphone',
'motion',
'reminders',
'siri',
'speech-recognition',
'camera',
'bluetooth',
'focus-status',
'user-tracking',
'health',
] as const;
export const privacySchema = z.object({
device: z
.string()
.describe('Device UDID, name, or "booted"'),
action: z
.enum(['grant', 'revoke', 'reset'])
.describe('Action to perform on the permission'),
service: z
.enum(privacyServices)
.describe('The privacy service/permission to modify'),
bundle_id: z
.string()
.describe('Bundle identifier of the app'),
});
export type PrivacyInput = z.infer<typeof privacySchema>;
export const privacyTool = {
name: 'simctl_privacy',
description: 'Grant, revoke, or reset privacy permissions for an app on a simulator',
inputSchema: privacySchema,
handler: async (input: PrivacyInput) => {
const udid = resolveDevice(input.device);
const action = input.action as PrivacyAction;
const service = input.service as PrivacyService;
const result = simctl('privacy', [udid, action, service, input.bundle_id]);
if (result.exitCode !== 0) {
throw new Error(`Failed to ${action} ${service} permission: ${result.stderr || result.stdout}`);
}
const actionPast = {
grant: 'granted',
revoke: 'revoked',
reset: 'reset',
}[action];
return {
content: [
{
type: 'text' as const,
text: `Successfully ${actionPast} ${service} permission for ${input.bundle_id} on device ${input.device}`,
},
],
};
},
};