// Copyright 2014 The Flutter Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

import '../../base/file_system.dart';
import '../../base/project_migrator.dart';
import '../../xcode_project.dart';

// Update the xcodeproj build location. Legacy build location does not work with Swift Packages.
class ProjectBuildLocationMigration extends ProjectMigrator {
  ProjectBuildLocationMigration(
    IosProject project,
    super.logger,
  ) : _xcodeProjectWorkspaceData = project.xcodeProjectWorkspaceData;

  final File _xcodeProjectWorkspaceData;

  @override
  bool migrate() {
    if (!_xcodeProjectWorkspaceData.existsSync()) {
      logger.printTrace('Xcode project workspace data not found, skipping build location migration.');
      return true;
    }

    processFileLines(_xcodeProjectWorkspaceData);
    return true;
  }

  @override
  String migrateLine(String line) {
    const String legacyBuildLocation = 'location = "group:Runner.xcodeproj"';
    const String defaultBuildLocation = 'location = "self:"';

    return line.replaceAll(legacyBuildLocation, defaultBuildLocation);
  }

  @override
  String migrateFileContents(String fileContents) {
    const String podLocation = '''
   <FileRef
      location = "group:Pods/Pods.xcodeproj">
   </FileRef>
''';

    return fileContents.replaceAll(podLocation, '');
  }
}
