import { createRequire as _createRequire } from 'node:module';
const require = _createRequire(import.meta.url);
import {
  require_normalize_path
} from "./chunk-ICJSZGEK.js";
import {
  getSourceFolder
} from "./chunk-DZ3U3XXP.js";
import {
  GridAppLayout
} from "./chunk-RTNIG6ZW.js";
import {
  isHttpApplicationError
} from "./chunk-HWT3LUJJ.js";
import {
  extractHttpError
} from "./chunk-TOBTRYZF.js";
import {
  DEPENDENCIES_METADATA_FILENAME,
  WIX_LOCK_FILENAME
} from "./chunk-YT5BPQDJ.js";
import {
  pathExists
} from "./chunk-VASURI4S.js";
import {
  CliError,
  CliErrorCode
} from "./chunk-MTT3QXBC.js";
import {
  __toESM,
  init_esm_shims
} from "./chunk-EXLZF52D.js";

// ../cli-site-old/src/utils.ts
init_esm_shims();
import { readdir, readFile, stat } from "node:fs/promises";
import { join } from "node:path";
async function readFiles(dir, subPath = "") {
  return (await Promise.all(
    (await readdir(join(dir, subPath))).map(async (file) => {
      const path = join(subPath, file);
      if ((await stat(join(dir, path))).isDirectory()) {
        return readFiles(dir, path);
      }
      return {
        path,
        content: await readFile(join(dir, path), "utf-8")
      };
    })
  )).flat();
}
function isSiteNotPublishedError(err) {
  const httpError = extractHttpError(err);
  if (httpError && isHttpApplicationError(httpError)) {
    const errorCode = httpError.response?.data.details.applicationError.code;
    if (errorCode === "SITE_NOT_PUBLISHED") {
      return true;
    }
  }
  return false;
}

// ../cli-site-old/src/gridapp/loadProjectGridAppContent.ts
init_esm_shims();
import { join as join2 } from "node:path";
import { readFile as readFile2 } from "node:fs/promises";
var import_normalize_path = __toESM(require_normalize_path(), 1);
async function readFileOrThrow(path) {
  try {
    return await readFile2(path, "utf-8");
  } catch (e) {
    throw new CliError({
      code: CliErrorCode.FailedToReadProjectFiles(),
      cause: e
    });
  }
}
async function readFilesOrThrow(root) {
  try {
    return await readFiles(root);
  } catch (e) {
    throw new CliError({
      code: CliErrorCode.FailedToReadProjectFiles(),
      cause: e
    });
  }
}
async function loadProjectRootFiles(projectFolder) {
  const [wixLockPath, dependenciesMetadataPath] = await Promise.all([
    pathExists(join2(projectFolder, WIX_LOCK_FILENAME)),
    pathExists(join2(projectFolder, DEPENDENCIES_METADATA_FILENAME))
  ]);
  if (wixLockPath) {
    return Promise.all(
      [
        "package.json",
        WIX_LOCK_FILENAME,
        ...dependenciesMetadataPath ? [DEPENDENCIES_METADATA_FILENAME] : []
      ].map(async (path) => {
        const content = await readFileOrThrow(join2(projectFolder, path));
        return { path, content };
      })
    );
  }
  return [];
}
async function loadProjectGridAppContent(projectFolder) {
  const sourceFiles = (await readFilesOrThrow(getSourceFolder(projectFolder))).map((file) => ({
    content: file.content,
    path: (0, import_normalize_path.default)(join2(getSourceFolder(""), file.path))
  }));
  const rootFiles = await loadProjectRootFiles(projectFolder);
  return {
    ignoreForbiddenPaths: true,
    layout: GridAppLayout.GITHUB,
    files: sourceFiles.concat(rootFiles)
  };
}

export {
  isSiteNotPublishedError,
  loadProjectGridAppContent
};
//# sourceMappingURL=chunk-4YAR43FD.js.map