"use strict";
// Copyright 2016-2018, Pulumi Corporation.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//     http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
    if (k2 === undefined) k2 = k;
    var desc = Object.getOwnPropertyDescriptor(m, k);
    if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
      desc = { enumerable: true, get: function() { return m[k]; } };
    }
    Object.defineProperty(o, k2, desc);
}) : (function(o, m, k, k2) {
    if (k2 === undefined) k2 = k;
    o[k2] = m[k];
}));
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
    Object.defineProperty(o, "default", { enumerable: true, value: v });
}) : function(o, v) {
    o["default"] = v;
});
var __importStar = (this && this.__importStar) || function (mod) {
    if (mod && mod.__esModule) return mod;
    var result = {};
    if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
    __setModuleDefault(result, mod);
    return result;
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.CosmosDBFunction = exports.CosmosChangeFeedSubscription = void 0;
const pulumi = __importStar(require("@pulumi/pulumi"));
const account_1 = require("./account");
const appservice = __importStar(require("../appservice"));
account_1.Account.prototype.onChange = function (name, args, opts) {
    return new CosmosChangeFeedSubscription(name, this, args, opts);
};
account_1.Account.prototype.getChangeFeedFunction = function (name, args) {
    return new CosmosDBFunction(name, { ...args, account: this });
};
class CosmosChangeFeedSubscription extends appservice.EventSubscription {
    account;
    constructor(name, account, args, opts = {}) {
        const resourceGroupName = appservice.getResourceGroupName(args, account.resourceGroupName);
        super("azure:eventhub:CosmosChangeFeedSubscription", name, new CosmosDBFunction(name, { ...args, account }), { ...args, resourceGroupName }, { parent: account, ...opts });
        this.account = account;
        this.registerOutputs();
    }
}
exports.CosmosChangeFeedSubscription = CosmosChangeFeedSubscription;
/**
 * Azure Function triggered by a Cosmos DB Change Feed.
 */
class CosmosDBFunction extends appservice.Function {
    constructor(name, args) {
        const bindingConnectionKey = pulumi.interpolate `Cosmos${args.account.name}ConnectionKey`;
        const trigger = {
            name: "items",
            direction: "in",
            type: "cosmosDBTrigger",
            connectionStringSetting: bindingConnectionKey,
            databaseName: args.databaseName,
            collectionName: args.collectionName,
            maxItemsPerInvocation: args.maxItemsPerInvocation,
            startFromBeginning: args.startFromBeginning,
            feedPollDelay: args.feedPollDelay,
            // We take an opiniated approach here: use the default "leases" collection as
            // a shared lease collection for all Cosmos DB triggered functions. With multiple
            // functions, this is both the simplest and the cheapest solution. The collection
            // will be auto-created if it doesn't exist yet.
            leaseCollectionPrefix: name,
            createLeaseCollectionIfNotExists: true,
        };
        // Place the mapping from the well known key name to the Cosmos DB connection string in
        // the 'app settings' object.
        const appSettings = pulumi.all([args.account.primarySqlConnectionString, bindingConnectionKey]).apply(([connectionString, key]) => ({ [key]: connectionString }));
        super(name, trigger, args, appSettings);
    }
}
exports.CosmosDBFunction = CosmosDBFunction;
//# sourceMappingURL=zMixins.js.map