# frozen_string_literal: true

# Freshdesk React Native SDK — iOS post_install helper.
#
# Embeds FreshdeskSDK.framework into the host app bundle when the SPM/default path is used.
# Required on React Native 0.75+ when FreshdeskReactNative uses spm_dependency (SPM does not
# auto-embed its products into the app). Safe to call in vendored mode too — the helper is
# idempotent and no-ops if FreshdeskSDK.framework is already handled.
#
# ── Default (SPM) Podfile ───────────────────────────────────────────────────
#
#   use_frameworks! :linkage => :dynamic
#
#   require_relative '../node_modules/@freshworks/react-native-freshdesk-sdk/ios/freshdesk_post_install'
#
#   post_install do |installer|
#     react_native_post_install(installer, config[:reactNativePath], :mac_catalyst_enabled => false)
#     freshdesk_post_install(installer)
#   end
#
# ── Vendored compatibility mode (no global dynamic frameworks) ──────────────
#
#   ENV['FRESHDESK_IOS_USE_VENDORED'] = '1'
#
#   require_relative '../node_modules/@freshworks/react-native-freshdesk-sdk/ios/freshdesk_post_install'
#
#   post_install do |installer|
#     react_native_post_install(installer, config[:reactNativePath], :mac_catalyst_enabled => false)
#     freshdesk_post_install(installer)  # optional but safe
#   end

def freshdesk_post_install(installer)
  embed_freshdesk_spm_framework(installer)
end

def embed_freshdesk_spm_framework(installer)
  freshdesk_install = '  install_framework "${BUILT_PRODUCTS_DIR}/FreshdeskSDK.framework"'
  target_support = File.join(installer.sandbox.root, 'Target Support Files')

  Dir.glob(File.join(target_support, 'Pods-*', 'Pods-*-frameworks.sh')).each do |script_path|
    script = File.read(script_path)
    next if script.include?('FreshdeskSDK.framework')

    updated = script.gsub(
      /(\s*install_framework "\$\{BUILT_PRODUCTS_DIR\}\/FreshdeskReactNative\/FreshdeskReactNative\.framework"\n)/,
      "\\1#{freshdesk_install}\n"
    )
    File.write(script_path, updated) if updated != script
  end

  Dir.glob(File.join(target_support, 'Pods-*', 'Pods-*-frameworks-*-input-files.xcfilelist')).each do |filelist|
    content = File.read(filelist)
    next if content.include?('FreshdeskSDK.framework')

    updated = content.gsub(
      "${BUILT_PRODUCTS_DIR}/FreshdeskReactNative/FreshdeskReactNative.framework\n",
      "${BUILT_PRODUCTS_DIR}/FreshdeskReactNative/FreshdeskReactNative.framework\n${BUILT_PRODUCTS_DIR}/FreshdeskSDK.framework\n"
    )
    File.write(filelist, updated) if updated != content
  end

  Dir.glob(File.join(target_support, 'Pods-*', 'Pods-*-frameworks-*-output-files.xcfilelist')).each do |filelist|
    content = File.read(filelist)
    next if content.include?('FreshdeskSDK.framework')

    updated = content.gsub(
      "${TARGET_BUILD_DIR}/${FRAMEWORKS_FOLDER_PATH}/FreshdeskReactNative.framework\n",
      "${TARGET_BUILD_DIR}/${FRAMEWORKS_FOLDER_PATH}/FreshdeskReactNative.framework\n${TARGET_BUILD_DIR}/${FRAMEWORKS_FOLDER_PATH}/FreshdeskSDK.framework\n"
    )
    File.write(filelist, updated) if updated != content
  end

  # SPM artifacts can be read-only; avoid codesign "Permission denied" during embed.
  Dir.glob(File.join(target_support, 'Pods-*', 'Pods-*-frameworks.sh')).each do |script_path|
    script = File.read(script_path)
    next if script.include?('freshdesk-chmod-before-codesign')

    updated = script.sub(
      '  code_sign_if_enabled "${destination}/$(basename "$1")"',
      "  # freshdesk-chmod-before-codesign\n  chmod -R u+w \"${destination}/$(basename \"$1\")\" 2>/dev/null || true\n  code_sign_if_enabled \"${destination}/$(basename \"$1\")\""
    )
    File.write(script_path, updated) if updated != script
  end
end
