"use strict";
/*
 * Copyright (c) 2018 by Filestack.
 * Some rights reserved.
 *
 * 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.
 */
Object.defineProperty(exports, "__esModule", { value: true });
var filestack_error_1 = require("./../../filestack_error");
var schema_1 = require("./../../schema");
var utils_1 = require("../utils");
/**
 * Returns Filestack base64 policy and HMAC-SHA256 signature
 *
 * ### Example
 *
 * ```js
 * import * as filestack from 'filestack-js';
 *
 * const jsonPolicy = { 'expiry': 253381964415 };
 * const security = filestack.getSecurity(jsonPolicy, '<YOUR_APP_SECRET>');
 * ```
 *
 * @param policyOptions
 * @param appSecret
 */
exports.getSecurity = function (policyOptions, appSecret) {
    if (!utils_1.isNode()) {
        throw new Error('getSecurity is only supported in nodejs');
    }
    var validateRes = schema_1.getValidator(schema_1.SecurityParamsSchema)(policyOptions);
    if (validateRes.errors.length) {
        throw new filestack_error_1.FilestackError("Invalid security params", validateRes.errors, filestack_error_1.FilestackErrorType.VALIDATION);
    }
    var policy = Buffer.from(JSON.stringify(policyOptions)).toString('base64');
    var signature = utils_1.requireNode('crypto').createHmac('sha256', appSecret)
        .update(policy)
        .digest('hex');
    return { policy: policy, signature: signature };
};
