import { createRequire as _createRequire } from 'node:module';
const require = _createRequire(import.meta.url);
import {
  useWixCliAstroBi
} from "./chunk-7LH6HYMZ.js";
import {
  useMultilingualClient
} from "./chunk-WNSL46BX.js";
import {
  TRANSLATIONS_FILE_PATH,
  getMultilingualMetadataPath,
  useProjectModel
} from "./chunk-RDF2RA4E.js";
import {
  SiteAuthProvider
} from "./chunk-HWT3LUJJ.js";
import {
  Alert,
  Box_default,
  ConfirmInput,
  Spinner,
  useAsync,
  useAsyncCallback
} from "./chunk-YL4347CI.js";
import {
  require_react
} from "./chunk-TODKQSCF.js";
import {
  pathExists,
  readFile,
  writeJson
} from "./chunk-VASURI4S.js";
import {
  external_exports
} from "./chunk-SDLCFZRL.js";
import {
  CliError,
  CliErrorCode,
  require_lib
} from "./chunk-MTT3QXBC.js";
import {
  __toESM,
  init_esm_shims
} from "./chunk-EXLZF52D.js";

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

// ../cli-astro-commands/src/components/TranslationPushCommand/TranslationPushCommand.tsx
init_esm_shims();
var import_react2 = __toESM(require_react(), 1);
var import_variant2 = __toESM(require_lib(), 1);
import { join } from "node:path";

// ../cli-astro-commands/src/components/TranslationPushCommand/TranslationPush.tsx
init_esm_shims();
var import_react = __toESM(require_react(), 1);
var import_variant = __toESM(require_lib(), 1);
var CONFIRM_PUSH_QUESTION = "Push translations? Unused keys will be deleted, new ones added. This can't be undone.";
var TranslationPush = ({
  translations,
  onComplete,
  skipConfirmation
}) => {
  const { model } = useProjectModel();
  const multilingualClient = useMultilingualClient();
  const bi = useWixCliAstroBi();
  const { status, execute } = useAsyncCallback(async () => {
    const primaryLanguageCode = await multilingualClient.getPrimaryLanguage();
    const metadataPath = getMultilingualMetadataPath(model.projectFolder);
    await writeJson(metadataPath, { primaryLanguageCode }, { spaces: 2 });
    await multilingualClient.pushTranslations(translations);
    return { primaryLanguageCode };
  });
  useAsync(async () => {
    if (!skipConfirmation) {
      return;
    }
    execute().then(() => onComplete?.()).catch(() => {
    });
  }, []);
  return /* @__PURE__ */ import_react.default.createElement(import_react.default.Fragment, null, !skipConfirmation && /* @__PURE__ */ import_react.default.createElement(Box_default, { marginBottom: 1 }, /* @__PURE__ */ import_react.default.createElement(
    ConfirmInput,
    {
      label: CONFIRM_PUSH_QUESTION,
      initialValue: true,
      onSubmit: (confirmed) => {
        bi.cliFlowStepAnswered({
          question: CONFIRM_PUSH_QUESTION,
          questionKey: "translation_push_command.confirm_push",
          answer: String(confirmed)
        });
        if (confirmed) {
          execute().then(() => onComplete?.()).catch(() => {
          });
        } else {
          onComplete?.();
        }
      }
    }
  )), (0, import_variant.match)(status, {
    NotRequested: () => null,
    Loading: () => /* @__PURE__ */ import_react.default.createElement(Spinner, { text: "Pushing translation file..." }),
    Success: ({ result: { primaryLanguageCode } }) => /* @__PURE__ */ import_react.default.createElement(Alert, { type: "success" }, 'Translation keys of "', primaryLanguageCode, '" were pushed.'),
    Error: () => null
  }));
};

// ../cli-astro-commands/src/components/TranslationPushCommand/read-translations-file.ts
init_esm_shims();
var translationsSchema = external_exports.union([
  external_exports.record(external_exports.string()),
  external_exports.record(external_exports.record(external_exports.string()))
]);
async function readTranslationsFile(translationsFilePath) {
  const fileContent = await readFile(translationsFilePath, "utf-8");
  let parsedContent;
  try {
    parsedContent = JSON.parse(fileContent);
  } catch (error) {
    throw new CliError({
      code: CliErrorCode.InvalidJsonFile({ filePath: translationsFilePath }),
      cause: error
    });
  }
  const parseResult = translationsSchema.safeParse(parsedContent);
  if (!parseResult.success) {
    throw new CliError({
      code: CliErrorCode.InvalidTranslationsFileSchema({
        filePath: translationsFilePath,
        zodError: parseResult.error
      }),
      cause: parseResult.error
    });
  }
  return parseResult.data;
}

// ../cli-astro-commands/src/components/TranslationPushCommand/TranslationPushCommand.tsx
var TranslationPushCommand = ({ onComplete, skipConfirmation }) => {
  const {
    model: { config, projectFolder }
  } = useProjectModel();
  const translationsFilePath = join(projectFolder, TRANSLATIONS_FILE_PATH);
  const { status } = useAsync(async () => {
    if (config.projectType !== "Site") {
      throw new CliError({
        code: CliErrorCode.TranslationUnsupportedForProjectType(),
        cause: null
      });
    }
    if (!await pathExists(translationsFilePath)) {
      throw new CliError({
        code: CliErrorCode.TranslationFileNotFound({
          filePath: TRANSLATIONS_FILE_PATH
        }),
        cause: null
      });
    }
    const translations = await readTranslationsFile(translationsFilePath);
    return { siteId: config.siteId, translations };
  }, [translationsFilePath]);
  return (0, import_variant2.match)(status, {
    Loading: () => null,
    Error: () => null,
    Success: ({ result: { siteId, translations } }) => {
      return /* @__PURE__ */ import_react2.default.createElement(SiteAuthProvider, { siteId }, /* @__PURE__ */ import_react2.default.createElement(
        TranslationPush,
        {
          translations,
          onComplete,
          skipConfirmation
        }
      ));
    }
  });
};

export {
  TranslationPushCommand
};
//# sourceMappingURL=chunk-K27CGPPG.js.map