var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
    return new (P || (P = Promise))(function (resolve, reject) {
        function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
        function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
        function step(result) { result.done ? resolve(result.value) : new P(function (resolve) { resolve(result.value); }).then(fulfilled, rejected); }
        step((generator = generator.apply(thisArg, _arguments || [])).next());
    });
};
(function (factory) {
    if (typeof module === "object" && typeof module.exports === "object") {
        var v = factory(require, exports);
        if (v !== undefined) module.exports = v;
    }
    else if (typeof define === "function" && define.amd) {
        define(["require", "exports", "../service", "./../spec/spec"], factory);
    }
})(function (require, exports) {
    "use strict";
    Object.defineProperty(exports, "__esModule", { value: true });
    const service_1 = require("../service");
    const spec_1 = require("./../spec/spec");
    /**
     * @hidden
     */
    const service = service_1.Service(spec_1.S3);
    /**
     * @hidden
     * @param param0
     */
    const Bucket = function (name, AWSClient, logical) {
        return new Promise((resolve, reject) => __awaiter(this, void 0, void 0, function* () {
            const client = new AWSClient.S3();
            const versioningPromise = client
                .getBucketVersioning({ Bucket: name })
                .promise();
            const corsPromise = client
                .getBucketCors({ Bucket: name })
                .promise()
                .then(data => {
                data.CORSRules = data.CORSRules.map(c => {
                    c.MaxAge = c.MaxAgeSeconds;
                    delete c.MaxAgeSeconds;
                    return c;
                });
                return data;
            })
                .catch(e => {
                // Silently catch the NoSuchCORSConfiguration
                return { CORSRules: null };
            });
            const lifecyclePromise = client
                .getBucketLifecycleConfiguration({ Bucket: name })
                .promise()
                .then(data => data)
                .catch(e => {
                // Silently catch the NoSuchLifecycleConfiguration
                return null;
            });
            const loggingPromise = client.getBucketLogging({ Bucket: name }).promise();
            const notificationPromise = client
                .getBucketNotification({ Bucket: name })
                .promise();
            const replicationPromise = client
                .getBucketReplication({ Bucket: name })
                .promise()
                .then(data => data)
                .catch(e => {
                // Silently catch the ReplicationConfigurationNotFoundError
                return null;
            });
            const taggingPromise = client
                .getBucketTagging({
                Bucket: name
            })
                .promise()
                .then(data => data)
                .catch(e => {
                // Silently catch the NoSuchTagSet
                return null;
            });
            const websitePromise = client
                .getBucketWebsite({ Bucket: name })
                .promise()
                .then(data => data)
                .catch(e => {
                // Silently catch the NoSuchWebsiteConfiguration
                return null;
            });
            const accessControlPromise = client
                .getBucketAcl({ Bucket: name })
                .promise();
            const acceleratePromise = client
                .getBucketAccelerateConfiguration({ Bucket: name })
                .promise();
            const analyticsPromise = client
                .listBucketAnalyticsConfigurations({ Bucket: name })
                .promise();
            const inventoryPromise = client
                .listBucketInventoryConfigurations({ Bucket: name })
                .promise();
            const metricsPromise = client
                .listBucketMetricsConfigurations({ Bucket: name })
                .promise();
            const [versionResults, corsResults, lifecycleResults, loggingResults, notificationResults, replicationResults, taggingResults, websiteResults, accessControlResults, accelerateResults, analyticsResults, inventoryResults, metricsResults] = yield Promise.all([
                versioningPromise,
                corsPromise,
                lifecyclePromise,
                loggingPromise,
                notificationPromise,
                replicationPromise,
                taggingPromise,
                websitePromise,
                accessControlPromise,
                acceleratePromise,
                analyticsPromise,
                inventoryPromise,
                metricsPromise
            ]);
            const resource = { BucketName: name };
            if (versionResults.Status) {
                resource.VersioningConfiguration = versionResults;
            }
            if (corsResults.CORSRules) {
                resource.CorsConfiguration = { CORSRules: corsResults.CORSRules };
            }
            if (lifecycleResults) {
                resource.LifecycleConfiguration = lifecycleResults;
            }
            if (loggingResults && loggingResults.LoggingEnabled) {
                resource.LoggingConfiguration = {
                    DestinationBucketName: loggingResults.LoggingEnabled.TargetBucket,
                    LogFilePrefix: loggingResults.LoggingEnabled.TargetPrefix
                };
            }
            if (Object.keys(notificationResults).length > 0) {
                resource.NotificationConfiguration = notificationResults;
            }
            if (replicationResults) {
                resource.ReplicationConfiguration = replicationResults;
            }
            if (taggingResults) {
                resource.Tags = taggingResults.TagSet;
            }
            if (websiteResults) {
                resource.WebsiteConfiguration = websiteResults;
            }
            if (accessControlResults) {
                resource.AccessControl = accessControlResults;
            }
            if (accelerateResults) {
                resource.AccelerateConfiguration = accelerateResults;
            }
            if (analyticsResults) {
                resource.AnalyticsConfigurations =
                    analyticsResults.AnalyticsConfigurationList;
            }
            if (inventoryResults) {
                resource.InventoryConfigurations =
                    inventoryResults.InventoryConfigurationList;
            }
            if (metricsResults) {
                resource.MetricsConfigurations = metricsResults.MetricsConfigurationList;
            }
            return resolve(service.Bucket(logical, resource));
        }));
    };
    /**
     * @hidden
     * @param param0
     */
    const BucketPolicy = function (name, AWSClient, logical) {
        return new Promise((resolve, reject) => __awaiter(this, void 0, void 0, function* () {
            const resourceClient = new AWSClient.S3();
            const { Policy } = yield resourceClient
                .getBucketPolicy({ Bucket: name })
                .promise();
            const resource = {
                Bucket: name,
                PolicyDocument: Policy
            };
            return resolve(service.BucketPolicy(logical, resource));
        }));
    };
    /**
     * @hidden
     */
    exports.S3 = {
        Bucket,
        BucketPolicy
    };
});
//# sourceMappingURL=S3.js.map