"use strict";
var __importDefault = (this && this.__importDefault) || function (mod) {
    return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.assetPath = exports.toBool = exports.toNumber = exports.getPixelConfig = exports.pixelDefaultConfig = exports.pixelDefaultAsset = exports.pixelDefaultEntity = void 0;
const path_1 = __importDefault(require("path"));
const fs_1 = require("fs");
const deepmerge_1 = __importDefault(require("deepmerge"));
exports.pixelDefaultEntity = {
    sprite: "",
    width: 16,
    height: 16,
};
exports.pixelDefaultAsset = {
    files: "*.{png,jpg}",
    output: "spritesheet.png",
    trim: true,
    padding: 2,
    tile: 0,
    extrude: true,
    skipExtension: true,
};
exports.pixelDefaultConfig = {
    define: {},
    build: {
        mode: "web",
    },
    project: {
        assets_folder: "src/assets",
        entities_folder: "src/entities",
    },
    entities: {},
    assets: {
        spritesheets: [],
    },
};
function getPixelConfig() {
    const p = path_1.default.resolve("package.json");
    if (!(0, fs_1.existsSync)(p))
        throw new Error("package.json not found !");
    const config = JSON.parse((0, fs_1.readFileSync)(p, "utf-8"));
    return (0, deepmerge_1.default)(exports.pixelDefaultConfig, config.pixel ?? {});
}
exports.getPixelConfig = getPixelConfig;
function toNumber(val, def) {
    if (val === undefined)
        return def;
    try {
        return parseInt(val.toString(), 10);
    }
    catch { }
    return def;
}
exports.toNumber = toNumber;
function toBool(val, def) {
    if (val === undefined)
        return def;
    return !!val;
}
exports.toBool = toBool;
function assetPath(config, filepath) {
    return path_1.default.join(config.project.assets_folder, filepath).replace(/\\/g, "/");
}
exports.assetPath = assetPath;
