summaryrefslogtreecommitdiff
path: root/Sources/BubombUI/Settings/PayloadEditorView.swift
blob: dd719591f07c2b76b698ff02fbe96662c701dc6f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
import SwiftUI
import BubombCore
#if canImport(AppKit)
import AppKit
import UniformTypeIdentifiers
#endif

/// Comprehensive editor for configuring Bubomb payloads.
@MainActor
public struct PayloadEditorView: View {
    @ObservedObject var settingsStore: SettingsStore
    @ObservedObject var executor: PayloadExecutor

    @State private var payloadName: String = ""
    @State private var selectedTypeIndex: Int = 0

    // Shell state
    @State private var shellCommand: String = ""
    @State private var shellPath: String = "/bin/zsh"
    @State private var shellWorkDir: String = ""
    @State private var shellArgs: String = ""
    @State private var shellTimeout: TimeInterval = 30
    @State private var envKeyInput: String = ""
    @State private var envValInput: String = ""
    @State private var envVars: [String: String] = [:]

    // App state
    @State private var appPath: String = ""
    @State private var appArgs: String = ""

    // URL state
    @State private var urlString: String = "https://"

    // Script state
    @State private var scriptPath: String = ""
    @State private var scriptArgs: String = ""
    @State private var scriptWorkDir: String = ""
    @State private var scriptTimeout: TimeInterval = 60

    // Sequence state
    @State private var sequenceSteps: [SequenceStep] = []
    @State private var sequenceStopOnError: Bool = true
    @State private var editingStepIndex: Int? = nil
    @State private var showStepEditor: Bool = false
    @State private var tempStep: SequenceStep = SequenceStep(name: "New Step", action: .openURL(urlString: "https://apple.com"))

    // Feedback & Validation
    @State private var feedbackMessage: String? = nil
    @State private var isErrorFeedback: Bool = false
    @State private var showDestructiveConfirmation: Bool = false
    @State private var pendingDestructiveActionReason: String? = nil

    public init(settingsStore: SettingsStore = .shared, executor: PayloadExecutor? = nil) {
        self.settingsStore = settingsStore
        self.executor = executor ?? PayloadExecutor.shared
    }

    public var body: some View {
        ScrollView {
            VStack(alignment: .leading, spacing: 18) {
                headerStatusSection
                payloadTypeSelector
                configurationFormSection
                safetyNoticeSection
                actionButtonsSection
            }
            .padding(18)
        }
        .background(BubombTheme.windowBackground)
        .onAppear {
            loadFromActivePayload()
        }
        .sheet(isPresented: $showStepEditor) {
            SequenceStepEditorView(
                step: $tempStep,
                onSave: {
                    if let idx = editingStepIndex, idx < sequenceSteps.count {
                        sequenceSteps[idx] = tempStep
                    } else {
                        sequenceSteps.append(tempStep)
                    }
                    showStepEditor = false
                },
                onCancel: {
                    showStepEditor = false
                }
            )
        }
        .alert("⚠️ Confirm Destructive Action", isPresented: $showDestructiveConfirmation) {
            Button("Cancel", role: .cancel) {}
            Button("Detonate Anyway", role: .destructive) {
                executeDetonation(bypassConfirmation: true)
            }
        } message: {
            Text(pendingDestructiveActionReason ?? "This action may perform irreversible changes.")
        }
    }

    // MARK: - Sections

    private var headerStatusSection: some View {
        HStack(spacing: 12) {
            VStack(alignment: .leading, spacing: 4) {
                HStack(spacing: 8) {
                    Text("Configured Payload:")
                        .font(.headline)
                        .foregroundColor(BubombTheme.textPrimary)

                    if let active = settingsStore.settings.activePayload {
                        HStack(spacing: 4) {
                            Circle().fill(BubombTheme.successGreen).frame(width: 8, height: 8)
                            Text(active.name)
                                .font(.subheadline.bold())
                                .foregroundColor(BubombTheme.accentOrange)
                        }
                    } else {
                        HStack(spacing: 4) {
                            Circle().fill(BubombTheme.accentAmber).frame(width: 8, height: 8)
                            Text("Unconfigured")
                                .font(.subheadline)
                                .foregroundColor(BubombTheme.textMuted)
                        }
                    }
                }

                Text("Configure the boom. One click on the menu bar bomb detonates this action instantly.")
                    .font(.caption)
                    .foregroundColor(BubombTheme.textSecondary)
            }

            Spacer()

            AnimatedBombView(size: 44, isDetonating: executor.currentState.isRunning) {
                detonateOrConfirm()
            }
        }
        .bubombCard()
    }

    private var payloadTypeSelector: some View {
        VStack(alignment: .leading, spacing: 10) {
            HStack {
                Text("Payload Name")
                    .font(.caption.bold())
                    .foregroundColor(BubombTheme.textSecondary)
                Spacer()
            }

            TextField("e.g. Build & Start Servers", text: $payloadName)
                .textFieldStyle(.roundedBorder)

            Text("Payload Type")
                .font(.caption.bold())
                .foregroundColor(BubombTheme.textSecondary)
                .padding(.top, 4)

            Picker("Type", selection: $selectedTypeIndex) {
                Text("Shell Command").tag(0)
                Text("Open Application").tag(1)
                Text("Open URL").tag(2)
                Text("Run Script").tag(3)
                Text("Multi-step Sequence").tag(4)
            }
            .pickerStyle(.segmented)
        }
        .bubombCard()
    }

    @ViewBuilder
    private var configurationFormSection: some View {
        VStack(alignment: .leading, spacing: 14) {
            switch selectedTypeIndex {
            case 0:
                shellConfigurationForm
            case 1:
                applicationConfigurationForm
            case 2:
                urlConfigurationForm
            case 3:
                scriptConfigurationForm
            case 4:
                sequenceConfigurationForm
            default:
                EmptyView()
            }
        }
        .bubombCard()
    }

    // MARK: - Type Specific Forms

    private var shellConfigurationForm: some View {
        VStack(alignment: .leading, spacing: 12) {
            Text("Shell Command Configuration")
                .font(.subheadline.bold())
                .foregroundColor(BubombTheme.accentOrange)

            VStack(alignment: .leading, spacing: 4) {
                Text("Command:").font(.caption).foregroundColor(BubombTheme.textSecondary)
                TextEditor(text: $shellCommand)
                    .font(.system(.body, design: .monospaced))
                    .frame(height: 70)
                    .padding(4)
                    .background(Color.black.opacity(0.3))
                    .cornerRadius(6)
                    .overlay(RoundedRectangle(cornerRadius: 6).stroke(BubombTheme.borderSubtle, lineWidth: 1))
            }

            HStack(spacing: 12) {
                VStack(alignment: .leading, spacing: 4) {
                    Text("Shell Environment:").font(.caption).foregroundColor(BubombTheme.textSecondary)
                    Picker("", selection: $shellPath) {
                        Text("Zsh (/bin/zsh)").tag("/bin/zsh")
                        Text("Bash (/bin/bash)").tag("/bin/bash")
                        Text("Sh (/bin/sh)").tag("/bin/sh")
                        Text("Direct Executable (no shell)").tag("")
                    }
                    .labelsHidden()
                }

                VStack(alignment: .leading, spacing: 4) {
                    Text("Timeout (Seconds):").font(.caption).foregroundColor(BubombTheme.textSecondary)
                    Picker("", selection: $shellTimeout) {
                        Text("5s").tag(TimeInterval(5))
                        Text("10s").tag(TimeInterval(10))
                        Text("30s (Default)").tag(TimeInterval(30))
                        Text("60s").tag(TimeInterval(60))
                        Text("120s").tag(TimeInterval(120))
                        Text("No Timeout").tag(TimeInterval(0))
                    }
                    .labelsHidden()
                }
            }

            VStack(alignment: .leading, spacing: 4) {
                Text("Working Directory:").font(.caption).foregroundColor(BubombTheme.textSecondary)
                HStack {
                    TextField("/Users/... (Leave empty for home)", text: $shellWorkDir)
                        .textFieldStyle(.roundedBorder)
                    Button("Browse...") {
                        chooseDirectory(target: $shellWorkDir)
                    }
                }
            }

            VStack(alignment: .leading, spacing: 4) {
                Text("Environment Variables:").font(.caption).foregroundColor(BubombTheme.textSecondary)
                HStack {
                    TextField("KEY", text: $envKeyInput)
                        .textFieldStyle(.roundedBorder)
                        .frame(width: 120)
                    TextField("VALUE", text: $envValInput)
                        .textFieldStyle(.roundedBorder)
                    Button("Add") {
                        let k = envKeyInput.trimmingCharacters(in: .whitespaces)
                        if !k.isEmpty {
                            envVars[k] = envValInput
                            envKeyInput = ""
                            envValInput = ""
                        }
                    }
                }

                if !envVars.isEmpty {
                    VStack(alignment: .leading, spacing: 4) {
                        ForEach(Array(envVars.keys.sorted()), id: \.self) { key in
                            HStack {
                                Text("\(key)=\(envVars[key] ?? "")")
                                    .font(.system(.caption, design: .monospaced))
                                    .foregroundColor(BubombTheme.accentPurple)
                                Spacer()
                                Button(action: { envVars.removeValue(forKey: key) }) {
                                    Image(systemName: "trash").font(.caption)
                                }
                                .buttonStyle(.plain)
                            }
                            .padding(.horizontal, 6)
                            .padding(.vertical, 2)
                            .background(Color.black.opacity(0.2))
                            .cornerRadius(4)
                        }
                    }
                    .padding(.top, 4)
                }
            }
        }
    }

    private var applicationConfigurationForm: some View {
        VStack(alignment: .leading, spacing: 12) {
            Text("Open Application Configuration")
                .font(.subheadline.bold())
                .foregroundColor(BubombTheme.accentOrange)

            VStack(alignment: .leading, spacing: 4) {
                Text("Target Application:").font(.caption).foregroundColor(BubombTheme.textSecondary)
                HStack {
                    TextField("/Applications/Xcode.app", text: $appPath)
                        .textFieldStyle(.roundedBorder)
                    Button("Choose App...") {
                        chooseApplication()
                    }
                }
            }

            VStack(alignment: .leading, spacing: 4) {
                Text("Optional Launch Arguments (Space separated):").font(.caption).foregroundColor(BubombTheme.textSecondary)
                TextField("--new-window", text: $appArgs)
                    .textFieldStyle(.roundedBorder)
            }
        }
    }

    private var urlConfigurationForm: some View {
        VStack(alignment: .leading, spacing: 12) {
            Text("Open URL Configuration")
                .font(.subheadline.bold())
                .foregroundColor(BubombTheme.accentOrange)

            VStack(alignment: .leading, spacing: 4) {
                Text("Web Destination URL:").font(.caption).foregroundColor(BubombTheme.textSecondary)
                HStack {
                    TextField("https://", text: $urlString)
                        .textFieldStyle(.roundedBorder)

                    // URL Validation badge
                    if case .success = PathValidator.validateURL(urlString) {
                        Image(systemName: "checkmark.circle.fill")
                            .foregroundColor(BubombTheme.successGreen)
                    } else {
                        Image(systemName: "exclamationmark.triangle.fill")
                            .foregroundColor(BubombTheme.failureRed)
                    }
                }
            }

            Text("Detonating this payload will instantly open the validated destination in your default browser.")
                .font(.caption)
                .foregroundColor(BubombTheme.textSecondary)
        }
    }

    private var scriptConfigurationForm: some View {
        VStack(alignment: .leading, spacing: 12) {
            Text("Run Script Configuration")
                .font(.subheadline.bold())
                .foregroundColor(BubombTheme.accentOrange)

            VStack(alignment: .leading, spacing: 4) {
                Text("Script File:").font(.caption).foregroundColor(BubombTheme.textSecondary)
                HStack {
                    TextField("/path/to/script.sh", text: $scriptPath)
                        .textFieldStyle(.roundedBorder)
                    Button("Browse...") {
                        chooseScript()
                    }
                }
            }

            VStack(alignment: .leading, spacing: 4) {
                Text("Arguments:").font(.caption).foregroundColor(BubombTheme.textSecondary)
                TextField("--env production --verbose", text: $scriptArgs)
                    .textFieldStyle(.roundedBorder)
            }

            VStack(alignment: .leading, spacing: 4) {
                Text("Working Directory:").font(.caption).foregroundColor(BubombTheme.textSecondary)
                HStack {
                    TextField("(Optional directory)", text: $scriptWorkDir)
                        .textFieldStyle(.roundedBorder)
                    Button("Browse...") {
                        chooseDirectory(target: $scriptWorkDir)
                    }
                }
            }
        }
    }

    private var sequenceConfigurationForm: some View {
        VStack(alignment: .leading, spacing: 12) {
            HStack {
                Text("Sequence Steps (\(sequenceSteps.count))")
                    .font(.subheadline.bold())
                    .foregroundColor(BubombTheme.accentOrange)
                Spacer()
                Button("+ Add Step") {
                    tempStep = SequenceStep(name: "Step \(sequenceSteps.count + 1)", action: .runCommand(command: "echo 'Step'", workingDirectory: nil, timeoutSeconds: 30))
                    editingStepIndex = nil
                    showStepEditor = true
                }
                .buttonStyle(.bordered)
            }

            Toggle("Stop sequence immediately if any step fails", isOn: $sequenceStopOnError)
                .font(.caption)
                .foregroundColor(BubombTheme.textSecondary)

            if sequenceSteps.isEmpty {
                Text("No steps configured. Click '+ Add Step' to build your multi-action detonation sequence.")
                    .font(.caption)
                    .foregroundColor(BubombTheme.textMuted)
                    .padding(.vertical, 8)
            } else {
                VStack(spacing: 6) {
                    ForEach(Array(sequenceSteps.enumerated()), id: \.element.id) { index, step in
                        HStack(spacing: 8) {
                            Text("\(index + 1).")
                                .font(.caption.bold())
                                .foregroundColor(BubombTheme.accentOrange)
                                .frame(width: 20)

                            VStack(alignment: .leading, spacing: 2) {
                                Text(step.name)
                                    .font(.subheadline.bold())
                                    .foregroundColor(BubombTheme.textPrimary)
                                Text("[\(step.actionTypeTitle)] \(step.summary)")
                                    .font(.caption)
                                    .foregroundColor(BubombTheme.textSecondary)
                                    .lineLimit(1)
                            }

                            Spacer()

                            Button(action: {
                                if index > 0 {
                                    sequenceSteps.swapAt(index, index - 1)
                                }
                            }) {
                                Image(systemName: "arrow.up").font(.caption)
                            }
                            .disabled(index == 0)

                            Button(action: {
                                if index < sequenceSteps.count - 1 {
                                    sequenceSteps.swapAt(index, index + 1)
                                }
                            }) {
                                Image(systemName: "arrow.down").font(.caption)
                            }
                            .disabled(index == sequenceSteps.count - 1)

                            Button(action: {
                                tempStep = step
                                editingStepIndex = index
                                showStepEditor = true
                            }) {
                                Image(systemName: "pencil").font(.caption)
                            }

                            Button(action: {
                                sequenceSteps.remove(at: index)
                            }) {
                                Image(systemName: "trash").font(.caption).foregroundColor(BubombTheme.failureRed)
                            }
                        }
                        .padding(8)
                        .background(Color.black.opacity(0.2))
                        .cornerRadius(6)
                    }
                }
            }
        }
    }

    // MARK: - Safety Notice

    private var safetyNoticeSection: some View {
        let currentPayload = assemblePayload()
        let risk = currentPayload.safetyRisk

        return Group {
            if risk.isDestructive {
                HStack(spacing: 8) {
                    Image(systemName: "exclamationmark.octagon.fill")
                        .foregroundColor(BubombTheme.failureRed)
                    Text("Destructive command detected: \(risk.warningMessage ?? ""). Bubomb will confirm before detonation.")
                        .font(.caption)
                        .foregroundColor(BubombTheme.failureRed)
                }
                .padding(10)
                .background(BubombTheme.failureRed.opacity(0.12))
                .cornerRadius(8)
            } else if let warning = risk.warningMessage {
                HStack(spacing: 8) {
                    Image(systemName: "exclamationmark.triangle.fill")
                        .foregroundColor(BubombTheme.accentAmber)
                    Text("Safety notice: \(warning)")
                        .font(.caption)
                        .foregroundColor(BubombTheme.accentAmber)
                }
                .padding(10)
                .background(BubombTheme.accentAmber.opacity(0.12))
                .cornerRadius(8)
            }
        }
    }

    // MARK: - Action Buttons

    private var actionButtonsSection: some View {
        VStack(spacing: 12) {
            if let feedback = feedbackMessage {
                HStack {
                    Image(systemName: isErrorFeedback ? "xmark.circle.fill" : "checkmark.circle.fill")
                        .foregroundColor(isErrorFeedback ? BubombTheme.failureRed : BubombTheme.successGreen)
                    Text(feedback)
                        .font(.caption.bold())
                        .foregroundColor(isErrorFeedback ? BubombTheme.failureRed : BubombTheme.successGreen)
                    Spacer()
                }
            }

            HStack(spacing: 14) {
                Button(action: {
                    savePayload()
                }) {
                    HStack {
                        Image(systemName: "checkmark")
                        Text("Save Payload")
                    }
                    .frame(maxWidth: .infinity)
                }
                .buttonStyle(.borderedProminent)
                .tint(BubombTheme.accentOrange)
                .keyboardShortcut("s", modifiers: .command)

                Button(action: {
                    detonateOrConfirm()
                }) {
                    HStack {
                        Image(systemName: "flame.fill")
                        Text(executor.currentState.isRunning ? "Detonating..." : "Test Payload")
                    }
                    .frame(maxWidth: .infinity)
                }
                .buttonStyle(.bordered)
                .tint(BubombTheme.accentAmber)
                .disabled(executor.currentState.isRunning)
                .keyboardShortcut(.return, modifiers: .command)
            }
        }
        .bubombCard()
    }

    // MARK: - Actions & File Pickers

    private func assemblePayload() -> Payload {
        let type: PayloadType
        switch selectedTypeIndex {
        case 0:
            let args = shellArgs.components(separatedBy: .whitespaces).filter { !$0.isEmpty }
            let dir = shellWorkDir.isEmpty ? nil : shellWorkDir
            let timeout = shellTimeout > 0 ? shellTimeout : nil
            type = .shellCommand(ShellConfig(
                command: shellCommand,
                shellPath: shellPath,
                arguments: args,
                workingDirectory: dir,
                environmentVariables: envVars,
                timeoutSeconds: timeout
            ))
        case 1:
            let args = appArgs.components(separatedBy: .whitespaces).filter { !$0.isEmpty }
            type = .openApplication(AppConfig(applicationPath: appPath, arguments: args))
        case 2:
            type = .openURL(URLConfig(urlString: urlString))
        case 3:
            let args = scriptArgs.components(separatedBy: .whitespaces).filter { !$0.isEmpty }
            let dir = scriptWorkDir.isEmpty ? nil : scriptWorkDir
            let timeout = scriptTimeout > 0 ? scriptTimeout : nil
            type = .runScript(ScriptConfig(
                scriptPath: scriptPath,
                arguments: args,
                workingDirectory: dir,
                timeoutSeconds: timeout
            ))
        case 4:
            type = .sequence(SequenceConfig(steps: sequenceSteps, stopOnError: sequenceStopOnError))
        default:
            type = .shellCommand(ShellConfig())
        }

        return Payload(
            name: payloadName.trimmingCharacters(in: .whitespaces).isEmpty ? "My Action" : payloadName,
            type: type
        )
    }

    private func savePayload() {
        let payload = assemblePayload()
        do {
            try payload.validate()
            settingsStore.setActivePayload(payload)
            feedbackMessage = "Payload '\(payload.name)' saved and armed!"
            isErrorFeedback = false
        } catch {
            feedbackMessage = error.localizedDescription
            isErrorFeedback = true
        }
    }

    private func detonateOrConfirm() {
        let payload = assemblePayload()
        do {
            try payload.validate()
        } catch {
            feedbackMessage = error.localizedDescription
            isErrorFeedback = true
            return
        }

        let alwaysConfirm = settingsStore.settings.confirmDestructiveActions
        let (requiresConfirm, reason) = SafetyPolicy.requiresConfirmation(
            command: shellCommand,
            userSettingAlwaysConfirmDestructive: alwaysConfirm || payload.requiresConfirmation
        )

        if requiresConfirm {
            pendingDestructiveActionReason = reason
            showDestructiveConfirmation = true
        } else {
            executeDetonation(bypassConfirmation: false)
        }
    }

    private func executeDetonation(bypassConfirmation: Bool) {
        let payload = assemblePayload()
        Task {
            do {
                _ = try await executor.detonate(payload: payload, ignoreSafetyConfirmation: bypassConfirmation)
                await MainActor.run {
                    feedbackMessage = "Detonation complete!"
                    isErrorFeedback = false
                }
            } catch {
                await MainActor.run {
                    feedbackMessage = "Detonation error: \(error.localizedDescription)"
                    isErrorFeedback = true
                }
            }
        }
    }

    private func loadFromActivePayload() {
        guard let payload = settingsStore.settings.activePayload else {
            payloadName = "Quick Build"
            shellCommand = "echo 'Boom! Bubomb is ready.'"
            return
        }

        payloadName = payload.name
        switch payload.type {
        case .shellCommand(let cfg):
            selectedTypeIndex = 0
            shellCommand = cfg.command
            shellPath = cfg.shellPath
            shellWorkDir = cfg.workingDirectory ?? ""
            shellArgs = cfg.arguments.joined(separator: " ")
            shellTimeout = cfg.timeoutSeconds ?? 0
            envVars = cfg.environmentVariables
        case .openApplication(let cfg):
            selectedTypeIndex = 1
            appPath = cfg.applicationPath
            appArgs = cfg.arguments.joined(separator: " ")
        case .openURL(let cfg):
            selectedTypeIndex = 2
            urlString = cfg.urlString
        case .runScript(let cfg):
            selectedTypeIndex = 3
            scriptPath = cfg.scriptPath
            scriptArgs = cfg.arguments.joined(separator: " ")
            scriptWorkDir = cfg.workingDirectory ?? ""
            scriptTimeout = cfg.timeoutSeconds ?? 0
        case .sequence(let cfg):
            selectedTypeIndex = 4
            sequenceSteps = cfg.steps
            sequenceStopOnError = cfg.stopOnError
        }
    }

    private func chooseApplication() {
        #if canImport(AppKit)
        let panel = NSOpenPanel()
        panel.canChooseFiles = true
        panel.canChooseDirectories = false
        panel.allowsMultipleSelection = false
        panel.allowedContentTypes = [.application]
        panel.directoryURL = URL(fileURLWithPath: "/Applications")
        if panel.runModal() == .OK, let url = panel.url {
            appPath = url.path
            if payloadName.isEmpty || payloadName == "Quick Build" {
                payloadName = url.deletingPathExtension().lastPathComponent
            }
        }
        #endif
    }

    private func chooseScript() {
        #if canImport(AppKit)
        let panel = NSOpenPanel()
        panel.canChooseFiles = true
        panel.canChooseDirectories = false
        panel.allowsMultipleSelection = false
        if panel.runModal() == .OK, let url = panel.url {
            scriptPath = url.path
            if payloadName.isEmpty || payloadName == "Quick Build" {
                payloadName = url.lastPathComponent
            }
        }
        #endif
    }

    private func chooseDirectory(target: Binding<String>) {
        #if canImport(AppKit)
        let panel = NSOpenPanel()
        panel.canChooseFiles = false
        panel.canChooseDirectories = true
        panel.allowsMultipleSelection = false
        if panel.runModal() == .OK, let url = panel.url {
            target.wrappedValue = url.path
        }
        #endif
    }
}