const assert = require('node:assert/strict');
const {assertExists} = require('../../../../utils/assertions');

const path = require('path');
const fs = require('fs').promises;
const os = require('os');
const Minifier = require('../../../../../core/frontend/services/assets-minification/minifier');

// minifier.getMatchingFiles() returns paths relative to process.cwd(); build
// the expected paths the same way so assertions do not assume the test runs
// with process.cwd() === ghost/core (the unified `pnpm test:watch` runs from
// the repo root).
const expectedFixturePath = file => path.relative(
    process.cwd(),
    path.join(__dirname, 'fixtures', 'basic-cards', 'css', file)
);

describe('Minifier', function () {
    let minifier;
    let testDir;

    beforeAll(async function () {
        testDir = await fs.mkdtemp(path.join(os.tmpdir(), 'minifier-tests-'));

        minifier = new Minifier({
            src: path.join(__dirname, 'fixtures', 'basic-cards'),
            dest: testDir
        });
    });

    afterAll(async function () {
        await fs.rm(testDir, {recursive: true, force: true});
    });

    describe('getMatchingFiles expands globs correctly', function () {
        it('star glob e.g. css/*.css', async function () {
            let result = await minifier.getMatchingFiles('css/*.css');

            assert(Array.isArray(result));
            assert.equal(result.length, 3);
            assert.equal(result[0], expectedFixturePath('bookmark.css'));
            assert.equal(result[1], expectedFixturePath('empty.css'));
            assert.equal(result[2], expectedFixturePath('gallery.css'));
        });

        it('match glob range e.g. css/bookmark.css and css/empty.css (css/@(bookmark|empty).css)', async function () {
            let result = await minifier.getMatchingFiles('css/@(bookmark|empty).css');

            assert(Array.isArray(result));
            assert.equal(result.length, 2);
            assert.equal(result[0], expectedFixturePath('bookmark.css'));
            assert.equal(result[1], expectedFixturePath('empty.css'));
        });

        it('reverse match glob e.g. css/!(bookmark).css', async function () {
            let result = await minifier.getMatchingFiles('css/!(bookmark).css');

            assert(Array.isArray(result));
            assert.equal(result.length, 2);
            assert.equal(result[0], expectedFixturePath('empty.css'));
            assert.equal(result[1], expectedFixturePath('gallery.css'));
        });
        it('reverse match glob e.g. css/!(bookmark|gallery).css', async function () {
            let result = await minifier.getMatchingFiles('css/!(bookmark|gallery).css');

            assert(Array.isArray(result));
            assert.equal(result.length, 1);
            assert.equal(result[0], expectedFixturePath('empty.css'));
        });
    });

    describe('Minify', function () {
        it('single type, single file', async function () {
            let result = await minifier.minify({
                'card.min.js': 'js/*.js'
            });
            assert(Array.isArray(result));
            assert.equal(result.length, 1);
        });

        it('single type, multi file', async function () {
            let result = await minifier.minify({
                'card.min.css': 'css/*.css'
            });
            assert(Array.isArray(result));
            assert.equal(result.length, 1);
        });

        it('both css and js types + multiple files', async function () {
            let result = await minifier.minify({
                'card.min.js': 'js/*.js',
                'card.min.css': 'css/*.css'
            });

            assert(Array.isArray(result));
            assert.equal(result.length, 2);
        });

        it('can replace the content', async function () {
            let result = await minifier.minify({
                'card.min.js': 'js/*.js'
            }, {
                replacements: {
                    '.kg-gallery-image': 'randomword'
                }
            });
            assert(Array.isArray(result));
            assert.equal(result.length, 1);

            const outputPath = minifier.getFullDest(result[0]);
            const content = await fs.readFile(outputPath, {encoding: 'utf8'});
            assert.match(content, /randomword/);
        });
    });

    describe('Bad inputs', function () {
        it('cannot create a minifier without src and dest', function () {
            assert.throws((function noObject(){
                new Minifier();
            }));

            assert.throws((function emptyObject() {
                new Minifier({});
            }));

            assert.throws((function missingSrc() {
                new Minifier({dest: 'a'});
            }));

            assert.throws((function missingDest() {
                new Minifier({src: 'a'});
            }));
        });

        it('can only handle css and js files', async function () {
            try {
                await minifier.minify({
                    'card.min.ts': 'js/*.ts'
                });
                assert.fail('Should have errored');
            } catch (err) {
                assertExists(err);
                assert.equal(err.errorType, 'IncorrectUsageError');
                assert.match(err.message, /Unexpected destination/);
            }
        });

        it('can handle missing files and folders gracefully', async function () {
            try {
                await minifier.minify({
                    'card.min.ts': 'ts/*.ts',
                    'card.min.js': 'js/fake.js'
                });
                assert.fail('Should have errored');
            } catch (err) {
                assertExists(err);
                assert.equal(err.errorType, 'IncorrectUsageError');
                assert.match(err.message, /Unable to read/);
            }
        });

        it('can minify empty js correctly to no result', async function () {
            let result = await minifier.minify({
                'card.min.js': 'js/empty.js'
            });

            assert.deepEqual(result, []);
        });

        it('can minify empty css correctly to no result', async function () {
            let result = await minifier.minify({
                'card.min.css': 'css/empty.css'
            });

            assert.deepEqual(result, []);
        });
    });
});
