import { createRequire as _createRequire } from 'node:module';
const require = _createRequire(import.meta.url);
import {
  useMultilingualClient
} from "./chunk-G2FHUVJQ.js";
import {
  MULTILINGUAL_METADATA_FILENAME,
  TRANSLATIONS_DIR,
  getTranslationsDir,
  useProjectModel
} from "./chunk-BYP4JZFS.js";
import {
  SiteAuthProvider
} from "./chunk-YHFCDWGT.js";
import {
  Alert,
  Spinner,
  useAsync
} from "./chunk-QK47PIWK.js";
import {
  require_react
} from "./chunk-TODKQSCF.js";
import {
  outputDir,
  outputFile,
  removeDir,
  writeJson
} from "./chunk-AYCGCYN3.js";
import {
  CliError,
  CliErrorCode,
  require_lib
} from "./chunk-WGV2XQSB.js";
import {
  __toESM,
  init_esm_shims
} from "./chunk-EXLZF52D.js";

// ../cli-astro-commands/src/components/TranslationPullCommand/index.ts
init_esm_shims();

// ../cli-astro-commands/src/components/TranslationPullCommand/TranslationPullCommand.tsx
init_esm_shims();
var import_react2 = __toESM(require_react(), 1);
var import_variant2 = __toESM(require_lib(), 1);

// ../cli-astro-commands/src/components/TranslationPullCommand/TranslationPull.tsx
init_esm_shims();
var import_react = __toESM(require_react(), 1);
var import_variant = __toESM(require_lib(), 1);

// ../cli-astro-commands/src/components/TranslationPullCommand/save-translation-files.ts
init_esm_shims();
import { join, dirname } from "node:path";
async function saveTranslationFiles(translationsDir, translationsByLanguage, primaryLanguage) {
  const multilingualDir = dirname(translationsDir);
  await removeDir(multilingualDir);
  await outputDir(translationsDir);
  for (const [locale, translations] of Object.entries(translationsByLanguage)) {
    const filePath = join(translationsDir, `${locale}.json`);
    await outputFile(filePath, JSON.stringify(translations, null, 2));
  }
  const metadataPath = join(multilingualDir, MULTILINGUAL_METADATA_FILENAME);
  await writeJson(
    metadataPath,
    { primaryLanguageCode: primaryLanguage },
    { spaces: 2 }
  );
}

// ../cli-astro-commands/src/components/TranslationPullCommand/TranslationPull.tsx
var TranslationPull = ({
  onComplete
}) => {
  const { model } = useProjectModel();
  const multilingualClient = useMultilingualClient();
  const { status } = useAsync(async () => {
    const [translationsByLanguage, primaryLanguage] = await Promise.all([
      multilingualClient.pullAllTranslations(),
      multilingualClient.getPrimaryLanguage()
    ]);
    const translationsDir = getTranslationsDir(model.projectFolder);
    await saveTranslationFiles(
      translationsDir,
      translationsByLanguage,
      primaryLanguage
    );
    onComplete?.();
  }, []);
  return (0, import_variant.match)(status, {
    Loading: () => /* @__PURE__ */ import_react.default.createElement(Spinner, { text: "Pulling translation files..." }),
    Success: () => {
      return /* @__PURE__ */ import_react.default.createElement(Alert, { type: "success" }, 'Translation files written to "', TRANSLATIONS_DIR, '"');
    },
    Error: () => null,
    NotRequested: () => null
  });
};

// ../cli-astro-commands/src/components/TranslationPullCommand/TranslationPullCommand.tsx
var TranslationPullCommand = ({
  onComplete
}) => {
  const {
    model: { config }
  } = useProjectModel();
  const { status } = useAsync(async () => {
    if (config.projectType !== "Site") {
      throw new CliError({
        code: CliErrorCode.TranslationUnsupportedForProjectType(),
        cause: null
      });
    }
    return { siteId: config.siteId };
  }, []);
  return (0, import_variant2.match)(status, {
    Loading: () => null,
    Error: () => null,
    Success: ({ result: { siteId } }) => {
      return /* @__PURE__ */ import_react2.default.createElement(SiteAuthProvider, { siteId }, /* @__PURE__ */ import_react2.default.createElement(TranslationPull, { onComplete }));
    }
  });
};

export {
  TranslationPullCommand
};
//# sourceMappingURL=chunk-VGNGBXHK.js.map