import {
  AWSClient
} from "./chunk-zqtpg06c.js";

// src/aws/ecr.ts
class ECRClient {
  client;
  region;
  constructor(region = "us-east-1", profile) {
    this.region = region;
    this.client = new AWSClient(undefined, { profile });
  }
  async createRepository(options) {
    const params = {
      repositoryName: options.repositoryName
    };
    if (options.imageTagMutability) {
      params.imageTagMutability = options.imageTagMutability;
    }
    if (options.imageScanningConfiguration) {
      params.imageScanningConfiguration = options.imageScanningConfiguration;
    }
    if (options.encryptionConfiguration) {
      params.encryptionConfiguration = options.encryptionConfiguration;
    }
    if (options.tags && options.tags.length > 0) {
      params.tags = options.tags;
    }
    const result = await this.client.request({
      service: "ecr",
      region: this.region,
      method: "POST",
      path: "/",
      headers: {
        "X-Amz-Target": "AmazonEC2ContainerRegistry_V20150921.CreateRepository",
        "Content-Type": "application/x-amz-json-1.1"
      },
      body: JSON.stringify(params)
    });
    return {
      repository: result.repository ? this.parseRepository(result.repository) : undefined
    };
  }
  async describeRepositories(options) {
    const params = {};
    if (options?.repositoryNames && options.repositoryNames.length > 0) {
      params.repositoryNames = options.repositoryNames;
    }
    if (options?.registryId) {
      params.registryId = options.registryId;
    }
    if (options?.maxResults) {
      params.maxResults = options.maxResults;
    }
    if (options?.nextToken) {
      params.nextToken = options.nextToken;
    }
    const result = await this.client.request({
      service: "ecr",
      region: this.region,
      method: "POST",
      path: "/",
      headers: {
        "X-Amz-Target": "AmazonEC2ContainerRegistry_V20150921.DescribeRepositories",
        "Content-Type": "application/x-amz-json-1.1"
      },
      body: JSON.stringify(params)
    });
    return {
      repositories: result.repositories?.map((repo) => this.parseRepository(repo)),
      nextToken: result.nextToken
    };
  }
  async getAuthorizationToken(registryIds) {
    const params = {};
    if (registryIds && registryIds.length > 0) {
      params.registryIds = registryIds;
    }
    const result = await this.client.request({
      service: "ecr",
      region: this.region,
      method: "POST",
      path: "/",
      headers: {
        "X-Amz-Target": "AmazonEC2ContainerRegistry_V20150921.GetAuthorizationToken",
        "Content-Type": "application/x-amz-json-1.1"
      },
      body: JSON.stringify(params)
    });
    return {
      authorizationData: result.authorizationData?.map((auth) => ({
        authorizationToken: auth.authorizationToken,
        expiresAt: auth.expiresAt,
        proxyEndpoint: auth.proxyEndpoint
      }))
    };
  }
  async deleteRepository(options) {
    const params = {
      repositoryName: options.repositoryName
    };
    if (options.registryId) {
      params.registryId = options.registryId;
    }
    if (options.force !== undefined) {
      params.force = options.force;
    }
    const result = await this.client.request({
      service: "ecr",
      region: this.region,
      method: "POST",
      path: "/",
      headers: {
        "X-Amz-Target": "AmazonEC2ContainerRegistry_V20150921.DeleteRepository",
        "Content-Type": "application/x-amz-json-1.1"
      },
      body: JSON.stringify(params)
    });
    return {
      repository: result.repository ? this.parseRepository(result.repository) : undefined
    };
  }
  async describeImages(options) {
    const params = {
      repositoryName: options.repositoryName
    };
    if (options.registryId) {
      params.registryId = options.registryId;
    }
    if (options.imageIds && options.imageIds.length > 0) {
      params.imageIds = options.imageIds;
    }
    if (options.filter) {
      params.filter = options.filter;
    }
    if (options.maxResults) {
      params.maxResults = options.maxResults;
    }
    if (options.nextToken) {
      params.nextToken = options.nextToken;
    }
    const result = await this.client.request({
      service: "ecr",
      region: this.region,
      method: "POST",
      path: "/",
      headers: {
        "X-Amz-Target": "AmazonEC2ContainerRegistry_V20150921.DescribeImages",
        "Content-Type": "application/x-amz-json-1.1"
      },
      body: JSON.stringify(params)
    });
    return {
      imageDetails: result.imageDetails?.map((img) => this.parseImageDetail(img)),
      nextToken: result.nextToken
    };
  }
  async batchDeleteImage(options) {
    const params = {
      repositoryName: options.repositoryName,
      imageIds: options.imageIds
    };
    if (options.registryId) {
      params.registryId = options.registryId;
    }
    const result = await this.client.request({
      service: "ecr",
      region: this.region,
      method: "POST",
      path: "/",
      headers: {
        "X-Amz-Target": "AmazonEC2ContainerRegistry_V20150921.BatchDeleteImage",
        "Content-Type": "application/x-amz-json-1.1"
      },
      body: JSON.stringify(params)
    });
    return {
      imageIds: result.imageIds,
      failures: result.failures
    };
  }
  async putLifecyclePolicy(options) {
    const params = {
      repositoryName: options.repositoryName,
      lifecyclePolicyText: options.lifecyclePolicyText
    };
    if (options.registryId) {
      params.registryId = options.registryId;
    }
    const result = await this.client.request({
      service: "ecr",
      region: this.region,
      method: "POST",
      path: "/",
      headers: {
        "X-Amz-Target": "AmazonEC2ContainerRegistry_V20150921.PutLifecyclePolicy",
        "Content-Type": "application/x-amz-json-1.1"
      },
      body: JSON.stringify(params)
    });
    return {
      registryId: result.registryId,
      repositoryName: result.repositoryName,
      lifecyclePolicyText: result.lifecyclePolicyText
    };
  }
  async getLifecyclePolicy(options) {
    const params = {
      repositoryName: options.repositoryName
    };
    if (options.registryId) {
      params.registryId = options.registryId;
    }
    const result = await this.client.request({
      service: "ecr",
      region: this.region,
      method: "POST",
      path: "/",
      headers: {
        "X-Amz-Target": "AmazonEC2ContainerRegistry_V20150921.GetLifecyclePolicy",
        "Content-Type": "application/x-amz-json-1.1"
      },
      body: JSON.stringify(params)
    });
    return {
      registryId: result.registryId,
      repositoryName: result.repositoryName,
      lifecyclePolicyText: result.lifecyclePolicyText,
      lastEvaluatedAt: result.lastEvaluatedAt
    };
  }
  async setRepositoryPolicy(options) {
    const params = {
      repositoryName: options.repositoryName,
      policyText: options.policyText
    };
    if (options.registryId) {
      params.registryId = options.registryId;
    }
    if (options.force !== undefined) {
      params.force = options.force;
    }
    const result = await this.client.request({
      service: "ecr",
      region: this.region,
      method: "POST",
      path: "/",
      headers: {
        "X-Amz-Target": "AmazonEC2ContainerRegistry_V20150921.SetRepositoryPolicy",
        "Content-Type": "application/x-amz-json-1.1"
      },
      body: JSON.stringify(params)
    });
    return {
      registryId: result.registryId,
      repositoryName: result.repositoryName,
      policyText: result.policyText
    };
  }
  async getRepositoryPolicy(options) {
    const params = {
      repositoryName: options.repositoryName
    };
    if (options.registryId) {
      params.registryId = options.registryId;
    }
    const result = await this.client.request({
      service: "ecr",
      region: this.region,
      method: "POST",
      path: "/",
      headers: {
        "X-Amz-Target": "AmazonEC2ContainerRegistry_V20150921.GetRepositoryPolicy",
        "Content-Type": "application/x-amz-json-1.1"
      },
      body: JSON.stringify(params)
    });
    return {
      registryId: result.registryId,
      repositoryName: result.repositoryName,
      policyText: result.policyText
    };
  }
  async tagResource(options) {
    const params = {
      resourceArn: options.resourceArn,
      tags: options.tags
    };
    await this.client.request({
      service: "ecr",
      region: this.region,
      method: "POST",
      path: "/",
      headers: {
        "X-Amz-Target": "AmazonEC2ContainerRegistry_V20150921.TagResource",
        "Content-Type": "application/x-amz-json-1.1"
      },
      body: JSON.stringify(params)
    });
  }
  async listTagsForResource(resourceArn) {
    const params = {
      resourceArn
    };
    const result = await this.client.request({
      service: "ecr",
      region: this.region,
      method: "POST",
      path: "/",
      headers: {
        "X-Amz-Target": "AmazonEC2ContainerRegistry_V20150921.ListTagsForResource",
        "Content-Type": "application/x-amz-json-1.1"
      },
      body: JSON.stringify(params)
    });
    return {
      tags: result.tags
    };
  }
  createLifecyclePolicyText(rules) {
    return JSON.stringify({
      rules: rules.map((rule) => ({
        rulePriority: rule.rulePriority,
        description: rule.description,
        selection: rule.selection,
        action: rule.action
      }))
    });
  }
  async getDockerLoginCommand() {
    const authResult = await this.getAuthorizationToken();
    if (!authResult.authorizationData?.[0]) {
      throw new Error("Failed to get authorization token");
    }
    const auth = authResult.authorizationData[0];
    const token = auth.authorizationToken || "";
    const endpoint = auth.proxyEndpoint || "";
    const decoded = Buffer.from(token, "base64").toString("utf8");
    const password = decoded.split(":")[1];
    return `echo "${password}" | docker login --username AWS --password-stdin ${endpoint}`;
  }
  getRegistryUri(accountId) {
    return `${accountId}.dkr.ecr.${this.region}.amazonaws.com`;
  }
  parseRepository(repo) {
    return {
      repositoryArn: repo.repositoryArn,
      registryId: repo.registryId,
      repositoryName: repo.repositoryName,
      repositoryUri: repo.repositoryUri,
      createdAt: repo.createdAt,
      imageTagMutability: repo.imageTagMutability,
      imageScanningConfiguration: repo.imageScanningConfiguration,
      encryptionConfiguration: repo.encryptionConfiguration
    };
  }
  parseImageDetail(img) {
    return {
      registryId: img.registryId,
      repositoryName: img.repositoryName,
      imageDigest: img.imageDigest,
      imageTags: img.imageTags,
      imageSizeInBytes: img.imageSizeInBytes,
      imagePushedAt: img.imagePushedAt,
      imageScanStatus: img.imageScanStatus,
      imageScanFindingsSummary: img.imageScanFindingsSummary
    };
  }
}

export { ECRClient };
