"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
const tslib_1 = require("tslib");
/*
 * Copyright (c) 2020, salesforce.com, inc.
 * All rights reserved.
 * Licensed under the BSD 3-Clause license.
 * For full license text, see LICENSE.txt file in the repo root or https://opensource.org/licenses/BSD-3-Clause
 */
const mime_types_1 = require("mime-types");
const path = require("path");
const i18n_1 = require("../i18n");
const utils_1 = require("../utils");
const baseGenerator_1 = require("./baseGenerator");
const EXTENSION_TEMPLATES = ['js', 'css', 'json', 'txt'];
class StaticResourceGenerator extends baseGenerator_1.BaseGenerator {
    validateOptions() {
        utils_1.CreateUtil.checkInputs(this.options.resourcename);
        if (!(0, mime_types_1.extension)(this.options.contenttype.toLowerCase())) {
            throw new Error(i18n_1.nls.localize('InvalidMimeType'));
        }
    }
    generate() {
        return tslib_1.__awaiter(this, void 0, void 0, function* () {
            const { resourcename, contenttype } = this.options;
            this.sourceRootWithPartialPath('staticresource');
            const ext = (0, mime_types_1.extension)(contenttype);
            if (ext && EXTENSION_TEMPLATES.includes(ext)) {
                // For types that we have default file, write that (js, css, txt, json)
                yield this.render(this.templatePath(`empty.${ext}`), this.destinationPath(path.join(this.outputdir, `${resourcename}.${ext}`)), {});
            }
            else if (ext === 'zip') {
                // For zip files, write an empty js file in a folder
                yield this.render(this.templatePath('_gitkeep'), this.destinationPath(path.join(this.outputdir, resourcename, '.gitkeep')), {});
            }
            else {
                // For all other mime types write a generic .resource file
                yield this.render(this.templatePath('empty.resource'), this.destinationPath(path.join(this.outputdir, `${resourcename}.resource`)), {});
            }
            yield this.render(this.templatePath('_staticresource.resource-meta.xml'), this.destinationPath(path.join(this.outputdir, `${resourcename}.resource-meta.xml`)), {
                contentType: contenttype,
            });
        });
    }
}
exports.default = StaticResourceGenerator;
//# sourceMappingURL=staticResourceGenerator.js.map