"use strict";
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
    if (k2 === undefined) k2 = k;
    var desc = Object.getOwnPropertyDescriptor(m, k);
    if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
      desc = { enumerable: true, get: function() { return m[k]; } };
    }
    Object.defineProperty(o, k2, desc);
}) : (function(o, m, k, k2) {
    if (k2 === undefined) k2 = k;
    o[k2] = m[k];
}));
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
    Object.defineProperty(o, "default", { enumerable: true, value: v });
}) : function(o, v) {
    o["default"] = v;
});
var __importStar = (this && this.__importStar) || function (mod) {
    if (mod && mod.__esModule) return mod;
    var result = {};
    if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
    __setModuleDefault(result, mod);
    return result;
};
var __importDefault = (this && this.__importDefault) || function (mod) {
    return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.TokenTranspiler = void 0;
const csstree = __importStar(require("css-tree"));
const css_to_react_native_transform_1 = __importDefault(require("css-to-react-native-transform"));
const lodash_1 = require("lodash");
class TokenTranspiler {
    parseRNValues(body) {
        const rBody = body.replace(/:\s*var\(\s*--/g, '_var:var(--');
        const tokens = (0, css_to_react_native_transform_1.default)(`.style${rBody}`).style;
        Object.keys(tokens).forEach(k => {
            if ((0, lodash_1.isString)(k) && (k.endsWith('Var') || k.endsWith('_var'))) {
                const normalizedKey = k.replace(/(Var|_var)$/, '');
                tokens[normalizedKey] = tokens[k];
                delete tokens[k];
            }
        });
        return tokens || {};
    }
    getColorSchemes(rnstylesheet) {
        const ast = csstree.parse(rnstylesheet);
        const colorSchemes = [];
        const variants = {};
        let currentScheme = {
            name: 'light',
            tokens: {}
        };
        colorSchemes.push(currentScheme);
        csstree.walk(ast, {
            enter: (node) => {
                if (node.type === 'Rule') {
                    const selector = csstree.generate(node.prelude).trim();
                    const body = csstree.generate(node.block || { type: 'Block', children: [] });
                    let fixedBody = body;
                    if (!/\{[\s]*\}/.test(fixedBody) && !fixedBody.endsWith(';}')) {
                        fixedBody = fixedBody.replace('}', ';}');
                    }
                    const tokens = this.parseRNValues(fixedBody);
                    if (selector === ':root') {
                        Object.assign(currentScheme.tokens, tokens);
                    }
                    else if (selector.startsWith('.')) {
                        const className = selector.replace(/^\./, '');
                        variants[className] = tokens;
                    }
                }
            }
        });
        return { colorSchemes, variants };
    }
}
exports.TokenTranspiler = TokenTranspiler;
//# sourceMappingURL=token.transpiler.js.map