"use strict";
/*
 * Copyright © 2019 Atomist, Inc.
 *
 * 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 });
exports.randomize = exports.mockGoalExecutor = exports.DefaultMockOptions = exports.MockGoalSize = void 0;
const _ = require("lodash");
/**
 * Enum to describe goal sizes when creating mock goal executors
 */
var MockGoalSize;
(function (MockGoalSize) {
    MockGoalSize[MockGoalSize["Small"] = 5] = "Small";
    MockGoalSize[MockGoalSize["Medium"] = 20] = "Medium";
    MockGoalSize[MockGoalSize["Large"] = 60] = "Large";
    MockGoalSize[MockGoalSize["ExtraLarge"] = 90] = "ExtraLarge";
})(MockGoalSize = exports.MockGoalSize || (exports.MockGoalSize = {}));
/**
 * Default mock options to be merged into custom options provided on configuration.sdm.mock
 */
exports.DefaultMockOptions = {
    enabled: false,
    defaultSize: MockGoalSize.Medium,
    goals: [],
    randomBy: 0.2,
};
/**
 * Create mock goal executor for a certain goal
 * @param goal
 * @param configuration
 */
function mockGoalExecutor(goal, sdmGoal, configuration) {
    const options = _.merge(exports.DefaultMockOptions, _.get(configuration, "sdm.mock", {}));
    let enabled;
    if (typeof options.enabled === "boolean") {
        enabled = options.enabled;
    }
    else {
        enabled = options.enabled(sdmGoal);
    }
    if (enabled) {
        const mock = options.goals.find(g => g.goal === goal);
        if (mock) {
            if (mock.mock) {
                return mock.mock;
            }
            else {
                return createGoalExecutor(randomize(mock.size, options.randomBy), mock.result);
            }
        }
        else {
            return createGoalExecutor(randomize(options.defaultSize, options.randomBy));
        }
    }
    return undefined;
}
exports.mockGoalExecutor = mockGoalExecutor;
function randomize(size, randomBy) {
    const base = getSize(size);
    const random = base * randomBy;
    const min = base - random;
    const max = base + random;
    return Math.floor(Math.random() * (max - min) + min);
}
exports.randomize = randomize;
function getSize(size) {
    if (typeof size === "number") {
        return size;
    }
    else {
        return +size;
    }
}
function createGoalExecutor(seconds, code = 0) {
    return async (gi) => {
        gi.progressLog.write(`Waiting for ${seconds}s`);
        await wait(seconds);
        gi.progressLog.write(`Finished waiting for ${seconds}s`);
        return {
            code,
        };
    };
}
function wait(seconds) {
    return new Promise(resolve => {
        setTimeout(resolve, seconds * 1000).unref();
    });
}
//# sourceMappingURL=mock.js.map