import { Policy } from '../iam/PolicyDocument';
import { join } from "../Value";
export class Website {
    aws;
    bucket;
    bucketPolicy;
    distribution;
    aRecord;
    constructor(aws, bucket, bucketPolicy, distribution, aRecord) {
        this.aws = aws;
        this.bucket = bucket;
        this.bucketPolicy = bucketPolicy;
        this.distribution = distribution;
        this.aRecord = aRecord;
    }
    withARecord(domain, hostedZoneId) {
        if (!this.distribution)
            throw new Error('You must call withDistribution to create A record');
        this.aRecord = this.aws.route53.recordSet({
            name: domain,
            type: 'A',
            hostedZoneId,
            aliasTarget: {
                dNSName: this.distribution.attributes.DomainName(),
                hostedZoneId: this.aws.cloudFrontHostedZoneId
            }
        });
        return this;
    }
    withDistribution(originId, certificateArn, aliases, rootObject = 'index.html', httpVersion = 'http2', edgeLambdaArn) {
        this.distribution = this.aws.cloudfront.distribution({
            distributionConfig: {
                aliases,
                customErrorResponses: [{ errorCode: 404, responseCode: 200, responsePagePath: '/' }],
                defaultCacheBehavior: {
                    cachePolicyId: '658327ea-f89d-4fab-a63d-7e88639e58f6',
                    compress: false,
                    ...(edgeLambdaArn ? { lambdaFunctionAssociations: [{
                                eventType: 'viewer-request',
                                lambdaFunctionARN: edgeLambdaArn,
                            }] } : {}),
                    responseHeadersPolicyId: '67f7725c-6f97-4210-82d7-5512b31e9d03',
                    targetOriginId: originId,
                    viewerProtocolPolicy: 'redirect-to-https'
                },
                defaultRootObject: rootObject,
                enabled: true,
                httpVersion: httpVersion,
                iPV6Enabled: true,
                origins: [
                    {
                        id: originId,
                        customOriginConfig: {
                            originProtocolPolicy: 'http-only',
                            originSSLProtocols: ['TLSv1.2'],
                        },
                        domainName: this.aws.functions.select(2, this.aws.functions.split('/', this.bucket.attributes.WebsiteURL())),
                    }
                ],
                viewerCertificate: {
                    acmCertificateArn: certificateArn,
                    minimumProtocolVersion: 'TLSv1.2_2021',
                    sslSupportMethod: 'sni-only'
                }
            }
        });
        return this;
    }
    static create(aws, bucketName, indexDocument = 'index.html', errorDocument = indexDocument) {
        const bucket = aws.s3.bucket({
            bucketName: bucketName,
            websiteConfiguration: { indexDocument, errorDocument },
            publicAccessBlockConfiguration: {
                blockPublicAcls: false,
                blockPublicPolicy: false,
                ignorePublicAcls: false,
                restrictPublicBuckets: false
            }
        });
        const policy = aws.s3.bucketPolicy({
            bucket: bucket,
            policyDocument: Policy.allow('s3:GetObject', [join(bucket.attributes.Arn(), '/*')], { principal: { AWS: '*' } }).document
        });
        return new Website(aws, bucket, policy);
    }
}
