"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 _ = require("lodash");
/** @internal */
exports.provider = 'firebase.auth';
/** Handle events in the Firebase Auth user lifecycle. */
function user() {
    return new UserBuilder('projects/' + process.env.GCLOUD_PROJECT);
}
exports.user = user;
var UserRecordMetadata = (function () {
    function UserRecordMetadata(creationTime, lastSignInTime) {
        this.creationTime = creationTime;
        this.lastSignInTime = lastSignInTime;
    }
    ;
    Object.defineProperty(UserRecordMetadata.prototype, "lastSignedInAt", {
        // Remove in v1.0.0
        /** @internal */
        get: function () {
            console.warn('WARNING: "lastSignedInAt" will be removed in firebase-functions v1.0.0. ' +
                'Please start using "lastSignInTime", which is an ISO string.');
            return new Date(this.lastSignInTime);
        },
        enumerable: true,
        configurable: true
    });
    Object.defineProperty(UserRecordMetadata.prototype, "createdAt", {
        // Remove in v1.0.0
        /** @internal */
        get: function () {
            console.warn('WARNING: "createdAt" will be removed in firebase-functions v1.0.0. ' +
                'Please start using "creationTime", which is an ISO string.');
            return new Date(this.creationTime);
        },
        enumerable: true,
        configurable: true
    });
    UserRecordMetadata.prototype.toJSON = function () {
        return {
            creationTime: this.creationTime,
            lastSignInTime: this.lastSignInTime,
        };
    };
    return UserRecordMetadata;
}());
exports.UserRecordMetadata = UserRecordMetadata;
/** Builder used to create Cloud Functions for Firebase Auth user lifecycle events. */
var UserBuilder = (function () {
    /** @internal */
    function UserBuilder(resource) {
        this.resource = resource;
    }
    UserBuilder.dataConstructor = function (raw) {
        // The UserRecord returned here is an interface. The firebase-admin/auth/user-record module
        // also has a class of the same name, which is one implementation of the interface.
        // Transform payload to firebase-admin v5.0.0 format
        var data = _.clone(raw.data);
        if (data.metadata) {
            var meta = data.metadata;
            data.metadata = new UserRecordMetadata(meta.createdAt || meta.creationTime, meta.lastSignedInAt || meta.lastSignInTime);
        }
        return data;
    };
    /** Respond to the creation of a Firebase Auth user. */
    UserBuilder.prototype.onCreate = function (handler) {
        return cloud_functions_1.makeCloudFunction({
            provider: exports.provider, handler: handler,
            resource: this.resource,
            eventType: 'user.create',
            dataConstructor: UserBuilder.dataConstructor,
        });
    };
    /** Respond to the deletion of a Firebase Auth user. */
    UserBuilder.prototype.onDelete = function (handler) {
        return cloud_functions_1.makeCloudFunction({
            provider: exports.provider, handler: handler,
            resource: this.resource,
            eventType: 'user.delete',
            dataConstructor: UserBuilder.dataConstructor,
        });
    };
    return UserBuilder;
}());
exports.UserBuilder = UserBuilder;
