// Copyright 2019-2024 Tauri Programme within The Commons Conservancy
// SPDX-License-Identifier: Apache-2.0
// SPDX-License-Identifier: MIT

use super::SectionItem;
use crate::helpers::config::ConfigMetadata;
use crate::helpers::framework;
use std::{fs::read_to_string, path::PathBuf};

pub fn items(config: &ConfigMetadata, frontend_dir: Option<&PathBuf>) -> Vec<SectionItem> {
  let mut items = Vec::new();
  let bundle_or_build = if config.bundle.active {
    "bundle"
  } else {
    "build"
  };
  items.push(SectionItem::new().description(format!("build-type: {bundle_or_build}")));

  let csp = config
    .app
    .security
    .csp
    .clone()
    .map(|c| c.to_string())
    .unwrap_or_else(|| "unset".to_string());
  items.push(SectionItem::new().description(format!("CSP: {csp}")));

  if let Some(frontend_dist) = &config.build.frontend_dist {
    items.push(SectionItem::new().description(format!("frontendDist: {frontend_dist}")));
  }

  if let Some(dev_url) = &config.build.dev_url {
    items.push(SectionItem::new().description(format!("devUrl: {dev_url}")));
  }

  if let Some(frontend_dir) = frontend_dir {
    if let Ok(package_json) = read_to_string(frontend_dir.join("package.json")) {
      let (framework, bundler) = framework::infer_from_package_json(&package_json);

      if let Some(framework) = framework {
        items.push(SectionItem::new().description(format!("framework: {framework}")));
      }

      if let Some(bundler) = bundler {
        items.push(SectionItem::new().description(format!("bundler: {bundler}")));
      }
    }
  }

  items
}
