#pragma once

#include <string>
#include <vector>

#ifdef _WIN32
#define WIN32_LEAN_AND_MEAN
#include <windows.h>
#include <shellapi.h>
#endif

namespace smartcut::core {
struct WindowsUtf8Argv {
  std::vector<std::string> storage;
  std::vector<char*> pointers;
};

inline WindowsUtf8Argv windows_utf8_argv() {
  WindowsUtf8Argv result;
#ifdef _WIN32
  int count = 0;
  wchar_t** arguments = CommandLineToArgvW(GetCommandLineW(), &count);
  if (!arguments) return result;
  result.storage.reserve(static_cast<std::size_t>(count));
  for (int i = 0; i < count; ++i) {
    const int bytes = WideCharToMultiByte(CP_UTF8, 0, arguments[i], -1, nullptr, 0, nullptr, nullptr);
    std::string value(bytes > 0 ? static_cast<std::size_t>(bytes) : 0, '\0');
    if (bytes > 0) { WideCharToMultiByte(CP_UTF8, 0, arguments[i], -1, value.data(), bytes, nullptr, nullptr); value.pop_back(); }
    result.storage.push_back(std::move(value));
  }
  LocalFree(arguments);
  result.pointers.reserve(result.storage.size());
  for (auto& value : result.storage) result.pointers.push_back(value.data());
#endif
  return result;
}
}  // namespace smartcut::core
