"use strict";
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
    function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
    return new (P || (P = Promise))(function (resolve, reject) {
        function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
        function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
        function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
        step((generator = generator.apply(thisArg, _arguments || [])).next());
    });
};
var __importDefault = (this && this.__importDefault) || function (mod) {
    return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
const ora_1 = __importDefault(require("ora"));
const fs_extra_1 = __importDefault(require("fs-extra"));
const schema_audit_1 = __importDefault(require("./schema-audit"));
const locales_audit_1 = __importDefault(require("./locales-audit"));
const snippets_audit_1 = __importDefault(require("./snippets-audit"));
// TODO: Add sections audit, will need to loop through .json & .liquid files and compare..
// creates the header for the audit file.
const createAuditHeader = () => {
    const date = new Date();
    const dateTime = {
        year: date.getFullYear(),
        month: date.getMonth() + 1,
        day: date.getDate(),
        hours: date.getHours(),
        minutes: date.getMinutes(),
        seconds: date.getSeconds(),
    };
    return `### Timestamp: ${dateTime.year}-${dateTime.month}-${dateTime.day} ${dateTime.hours}:${dateTime.minutes}:${dateTime.seconds} \n---\n`;
};
// creates the footer for the audit file.
const createAuditFooter = () => `\n---\n#### Audited by mango 🥭`;
// creates the audit file.
const writeAuditFile = (data) => __awaiter(void 0, void 0, void 0, function* () {
    if (yield fs_extra_1.default.pathExists('./mango-audit.md')) {
        yield fs_extra_1.default.remove('./mango-audit.md');
    }
    yield fs_extra_1.default.writeFile('./mango-audit.md', createAuditHeader() + data + createAuditFooter());
});
// reusuable method to create audit content to be written. 
const createSettingsAudit = (data, result, type, symbol) => {
    const lines = [];
    data.forEach((string) => lines.push(`- ${symbol} ${string} ${result}`));
    return `#### ${type}\n\n---\n${lines.join('\n')}\n---\n`;
};
// main audit func gets ran in cli.ts
const audit = () => __awaiter(void 0, void 0, void 0, function* () {
    const spinner = (0, ora_1.default)('Auditing theme files').start();
    try {
        const schemaAudit = yield (0, schema_audit_1.default)(spinner);
        const snippetsAudit = yield (0, snippets_audit_1.default)(spinner);
        const localesAudit = yield (0, locales_audit_1.default)(spinner);
        const schemaResults = `
    ${createSettingsAudit(schemaAudit, 'failed', '🥭 settings_schema.json', '👾')}
    ${createSettingsAudit(snippetsAudit, 'not in use', '🥭 snippets', '✂️')}
    ${createSettingsAudit(localesAudit, 'not found', '🥭 locales.default.json', '📘')}
    `;
        yield writeAuditFile(schemaResults);
        spinner.stopAndPersist({
            symbol: schemaAudit.length >= 1 ? '⛔️' : '🥭',
            text: `There are ${schemaAudit.length} settings unused`,
        });
        spinner.stopAndPersist({
            symbol: snippetsAudit.length >= 1 ? '⛔️' : '✂️',
            text: `There are ${snippetsAudit.length} snippets unused`,
        });
        spinner.stopAndPersist({
            symbol: localesAudit.length >= 1 ? '⛔️' : '🤟',
            text: `There are ${localesAudit.length} locales unused`,
        });
        spinner.succeed('Finished auditing theme files, check mango-audit.md for output');
    }
    catch (err) {
        spinner.fail('Error auditing theme files, check the output below');
        console.error(err);
        process.exit(1);
    }
});
exports.default = audit;
