"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;
    };
})();
var __importDefault = (this && this.__importDefault) || function (mod) {
    return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.FileStateAdapter = void 0;
const fs_1 = __importDefault(require("fs"));
const path = __importStar(require("path"));
class FileStateAdapter {
    constructor(config) {
        this.filePath = path.join(config.filePath, 'motia.state.json');
    }
    async init() {
        const dir = this.filePath.replace('motia.state.json', '');
        try {
            fs_1.default.realpathSync(dir);
        }
        catch {
            fs_1.default.mkdirSync(dir, { recursive: true });
        }
        try {
            fs_1.default.readFileSync(this.filePath, 'utf-8');
        }
        catch {
            fs_1.default.writeFileSync(this.filePath, JSON.stringify({}), 'utf-8');
        }
    }
    async get(traceId, key) {
        const data = this._readFile();
        const fullKey = this._makeKey(traceId, key);
        return data[fullKey] ? JSON.parse(data[fullKey]) : null;
    }
    async set(traceId, key, value) {
        const data = this._readFile();
        const fullKey = this._makeKey(traceId, key);
        data[fullKey] = JSON.stringify(value);
        this._writeFile(data);
        return value;
    }
    async delete(traceId, key) {
        const data = this._readFile();
        const fullKey = this._makeKey(traceId, key);
        const value = await this.get(traceId, key);
        if (value) {
            delete data[fullKey];
            this._writeFile(data);
        }
        return value;
    }
    async clear(traceId) {
        const data = this._readFile();
        const pattern = this._makeKey(traceId, '');
        for (const key in data) {
            if (key.startsWith(pattern)) {
                delete data[key];
            }
        }
        this._writeFile(data);
    }
    async keys(traceId) {
        const data = this._readFile();
        return Object.keys(data)
            .filter((key) => key.startsWith(this._makeKey(traceId, '')))
            .map((key) => key.replace(this._makeKey(traceId, ''), ''));
    }
    async traceIds() {
        const data = this._readFile();
        const traceIds = new Set();
        Object.keys(data).forEach((key) => traceIds.add(key.split(':')[0]));
        return Array.from(traceIds);
    }
    async cleanup() {
        // No cleanup needed for file system
    }
    _makeKey(traceId, key) {
        return `${traceId}:${key}`;
    }
    _readFile() {
        const content = fs_1.default.readFileSync(this.filePath, 'utf-8');
        return JSON.parse(content);
    }
    _writeFile(data) {
        fs_1.default.writeFileSync(this.filePath, JSON.stringify(data, null, 2), 'utf-8');
    }
}
exports.FileStateAdapter = FileStateAdapter;
