"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 () {
    var ownKeys = function(o) {
        ownKeys = Object.getOwnPropertyNames || function (o) {
            var ar = [];
            for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k;
            return ar;
        };
        return ownKeys(o);
    };
    return function (mod) {
        if (mod && mod.__esModule) return mod;
        var result = {};
        if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]);
        __setModuleDefault(result, mod);
        return result;
    };
})();
Object.defineProperty(exports, "__esModule", { value: true });
exports.getDefaultsFromScriptSetup = getDefaultsFromScriptSetup;
exports.resolveDefaultOptionExpression = resolveDefaultOptionExpression;
const core = __importStar(require("@vue/language-core"));
function getDefaultsFromScriptSetup(ts, printer, sourceScript) {
    const virtualCode = sourceScript?.generated?.root;
    if (!virtualCode) {
        return;
    }
    const sourceFile = virtualCode.sfc.scriptSetup?.ast;
    if (!sourceFile) {
        return;
    }
    const scriptSetupRanges = core.parseScriptSetupRanges(ts, sourceFile, virtualCode.vueCompilerOptions);
    if (scriptSetupRanges) {
        return collectPropDefaultsFromScriptSetup(ts, printer, sourceFile, scriptSetupRanges);
    }
}
function collectPropDefaultsFromScriptSetup(ts, printer, sourceFile, scriptSetupRanges) {
    const result = new Map();
    if (scriptSetupRanges.withDefaults?.arg) {
        const obj = findObjectLiteralExpression(ts, scriptSetupRanges.withDefaults.arg.node);
        if (obj) {
            for (const prop of obj.properties) {
                if (ts.isPropertyAssignment(prop)) {
                    const name = prop.name.getText(sourceFile);
                    const expNode = resolveDefaultOptionExpression(ts, prop.initializer);
                    const expText = printer.printNode(ts.EmitHint.Expression, expNode, sourceFile);
                    result.set(name, expText);
                }
            }
        }
    }
    else if (scriptSetupRanges.defineProps?.destructured) {
        for (const [name, initializer] of scriptSetupRanges.defineProps.destructured) {
            if (initializer) {
                const expText = printer.printNode(ts.EmitHint.Expression, initializer, sourceFile);
                result.set(name, expText);
            }
        }
    }
    if (scriptSetupRanges.defineModel) {
        for (const defineModel of scriptSetupRanges.defineModel) {
            const obj = defineModel.arg ? findObjectLiteralExpression(ts, defineModel.arg.node) : undefined;
            if (obj) {
                const name = defineModel.name
                    ? sourceFile.text.slice(defineModel.name.start, defineModel.name.end).slice(1, -1)
                    : 'modelValue';
                const _default = resolveModelOption(ts, printer, sourceFile, obj);
                if (_default) {
                    result.set(name, _default);
                }
            }
        }
    }
    return result;
}
function findObjectLiteralExpression(ts, node) {
    if (ts.isObjectLiteralExpression(node)) {
        return node;
    }
    let result;
    node.forEachChild(child => {
        if (!result) {
            result = findObjectLiteralExpression(ts, child);
        }
    });
    return result;
}
function resolveModelOption(ts, printer, sourceFile, options) {
    let _default;
    for (const prop of options.properties) {
        if (ts.isPropertyAssignment(prop)) {
            const name = prop.name.getText(sourceFile);
            if (name === 'default') {
                const expNode = resolveDefaultOptionExpression(ts, prop.initializer);
                const expText = printer.printNode(ts.EmitHint.Expression, expNode, sourceFile) ?? expNode.getText(sourceFile);
                _default = expText;
            }
        }
    }
    return _default;
}
function resolveDefaultOptionExpression(ts, _default) {
    if (ts.isArrowFunction(_default)) {
        if (ts.isBlock(_default.body)) {
            return _default; // TODO
        }
        else if (ts.isParenthesizedExpression(_default.body)) {
            return _default.body.expression;
        }
        else {
            return _default.body;
        }
    }
    return _default;
}
//# sourceMappingURL=scriptSetup.js.map