import {
  DeployManager,
  InvalidRequest3,
  NotFoundError,
  S3Client2
} from "../chunk-ZBT5SDIQ.js";
import "../chunk-V7TRGEV6.js";
import "../chunk-3AVBP57V.js";
import "../chunk-ZIHRRTTA.js";
import "../chunk-3JKQPIJ2.js";
import "../chunk-7NN7DVGK.js";
import "../chunk-63Q3RLAK.js";
import "../chunk-OCHPLE5N.js";
import "../chunk-GJFKL6XM.js";
import "../chunk-24XYHOWM.js";
import "../chunk-57WYIP4I.js";
import "../chunk-TDZTH7TL.js";
import "../chunk-RYIGWCGK.js";
import "../chunk-H4QJ5O4E.js";
import "../chunk-THUA35UY.js";

// src/lambda/utils/parseAppName.ts
function parseAppName(uri) {
  const [, , appName] = uri.split("/");
  return appName;
}

// src/lambda/utils/parseGroupId.ts
function parseGroupId(uri) {
  const [, , , groupId] = uri.split("/");
  return groupId;
}

// src/lambda/utils/parsePlatform.ts
function parsePlatform(uri) {
  if (uri.startsWith("/ios")) {
    return "ios";
  }
  if (uri.startsWith("/android")) {
    return "android";
  }
  throw new InvalidRequest3(`URI must start with /ios or /android. uri: ${uri}`);
}

// src/lambda/utils/parseSuffix.ts
function parseSuffix(uri) {
  const [, , , , tag] = uri.split("/");
  if (tag == null || tag?.length === 0) {
    throw new InvalidRequest3(`unable to parse suffix: ${uri}`);
  }
  return tag;
}

// src/lambda/origin-request.ts
function createOriginRequestHandler(context) {
  const s3Client = new S3Client2({
    bucket: context.bucketName,
    region: context.region
  });
  return async function handler2(event) {
    try {
      const request = event.Records[0]?.cf?.request;
      if (request == null) {
        throw new InvalidRequest3("request is null");
      }
      const appName = parseAppName(request.uri);
      const platform = parsePlatform(request.uri);
      const groupId = parseGroupId(request.uri);
      const suffix = parseSuffix(request.uri);
      if (appName == null || platform == null || groupId == null) {
        throw new InvalidRequest3("invalid request");
      }
      const deploymentId = await DeployManager.resolveDeploymentId(
        {
          appName,
          groupId,
          allowAccessCluster: context.allowAccessCluster
        },
        {
          s3Client
        }
      );
      const bundlePath = DeployManager.resolveBundle({
        appName,
        platform,
        deploymentId,
        tag: suffix !== "bundle" ? suffix : void 0
      });
      const absolutePath = `/${bundlePath}`;
      request.uri = absolutePath;
      request.headers["x-bundle"] = [
        {
          key: "X-Bundle",
          value: absolutePath
        }
      ];
      return request;
    } catch (error) {
      if (error instanceof NotFoundError) {
        return { status: "404", statusDescription: error.message };
      }
      if (error instanceof InvalidRequest3) {
        return { status: "400", statusDescription: error.message };
      }
      throw error;
    }
  };
}
var handler = createOriginRequestHandler({
  allowAccessCluster: false,
  bucketName: _BUCKET_NAME,
  region: _BUCKET_REGION
});
export {
  createOriginRequestHandler,
  handler
};
