import util from 'util';
import { Callable } from "./Callable";
import Class from './Class';

export default class Instance {
    fields: Map<string, any> = new Map();
    methods: Map<string, Callable> = new Map();

    readonly klass: Class;

    constructor(klass: Class) {
        this.klass = klass;
        this.fields = this.klass.properties;
    }

    get(name: string) {
        if(this.fields.has(name)) {
            return this.fields.get(name);
        }

        let method = this.klass.findMethod(name);
        if(method != null) return method.bind(this);

        throw new String(`Undefined property '${name}'.`);
    }

    set(name: string, value: any) {
        this.fields.set(name, value);
    }

    toString() {
        return this.klass.name + " instance";
    }

    [util.inspect.custom]() {
        return this.toString();
    }
}