"use strict";
// The MIT License (MIT)
//
// Copyright (c) 2017 Firebase
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in all
// copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
// SOFTWARE.
Object.defineProperty(exports, "__esModule", { value: true });
var cloud_functions_1 = require("../cloud-functions");
var index_1 = require("../index");
/** @internal */
exports.provider = 'cloud.storage';
/**
 * The optional bucket function allows you to choose which buckets' events to handle.
 * This step can be bypassed by calling object() directly, which will use the bucket that
 * the Firebase SDK for Cloud Storage uses.
 */
function bucket(bucket) {
    if (!/^[a-z\d][a-z\d\\._-]{1,230}[a-z\d]$/.test(bucket)) {
        throw new Error('Invalid bucket name ${bucket}');
    }
    return new BucketBuilder("projects/_/buckets/" + bucket);
}
exports.bucket = bucket;
function object() {
    return bucket(index_1.config().firebase.storageBucket).object();
}
exports.object = object;
var BucketBuilder = (function () {
    /** @internal */
    function BucketBuilder(resource) {
        this.resource = resource;
    }
    /** Handle events for objects in this bucket. */
    BucketBuilder.prototype.object = function () {
        return new ObjectBuilder(this.resource);
    };
    return BucketBuilder;
}());
exports.BucketBuilder = BucketBuilder;
// A RegExp that matches on a GCS media link that points at a _nested_ object (one that uses a path/with/slashes).
var nestedMediaLinkRE = new RegExp('https://www.googleapis.com/storage/v1/b/([^/]+)/o/(.*)');
var ObjectBuilder = (function () {
    /** @internal */
    function ObjectBuilder(resource) {
        this.resource = resource;
    }
    /**
     * Handle any change to any object.
     */
    ObjectBuilder.prototype.onChange = function (handler) {
        // This is a temporary shim to fix the 'mediaLink' for nested objects.
        // BUG(37962789): clean this up when backend fix for bug is deployed.
        var correctMediaLink = function (event) {
            var deconstructedNestedLink = event.data.mediaLink.match(nestedMediaLinkRE);
            if (deconstructedNestedLink != null) {
                // The media link for a nested object uses an illegal URL (using literal slashes instead of "%2F".
                // Fix up the URL.
                var bucketName = deconstructedNestedLink[1];
                var fixedTail = deconstructedNestedLink[2].replace(/\//g, '%2F'); // "/\//g" means "all forward slashes".
                event.data.mediaLink = 'https://www.googleapis.com/storage/v1/b/' + bucketName + '/o/' + fixedTail;
            }
            return handler(event);
        };
        return cloud_functions_1.makeCloudFunction({ provider: exports.provider, handler: correctMediaLink, resource: this.resource, eventType: 'object.change' });
    };
    return ObjectBuilder;
}());
exports.ObjectBuilder = ObjectBuilder;
