import * as yayson from 'yayson';
import { Util } from '../util/util';
const singleton = Symbol();
const singletonEnforcer = Symbol();
export class ConversionService {
    constructor(enforcer) {
        /*
    
    The members data and errors MUST NOT coexist in the same document.
    
    A document MAY contain any of these top-level members:
    
    jsonapi: an object describing the server’s implementation
    links: a links object related to the primary data.
    included: an array of resource objects that are related to the primary data and/or each other (“included resources”).
    If a document does not contain a top-level data key, the included member MUST NOT be present either.
    
    The top-level links object MAY contain the following members:
    
    self: the link that generated the current response document.
    related: a related resource link when the primary data represents a resource relationship.
    pagination links for the primary data.
    The document’s “primary data” is a representation of the resource or collection of resources targeted by a request.
    
    Primary data MUST be either:
    
    a single resource object, a single resource identifier object, or null, for requests that target single resources
    an array of resource objects, an array of resource identifier objects, or an empty array ([]), for requests that target resource collections
    For example, the following primary data is a single resource object:
    
    // SERVER / CLIENT
    
    Client Responsibilities
    Clients MUST send all JSON API data in request documents with the header
    Content-Type: application/vnd.api+json without any media type parameters.
    
    Clients that include the JSON API media type in their Accept header MUST specify the media type there at least once without any media type parameters.
    
    Clients MUST ignore any parameters for the application/vnd.api+json media type received in the Content-Type header of response documents.
    
    Server Responsibilities
    Servers MUST send all JSON API data in response documents with the header Content-Type: application/vnd.api+json without any media type parameters.
    
    Servers MUST respond with a 415 Unsupported Media Type status code if a request specifies the header Content-Type: application/vnd.api+json with any media type parameters.
    
    Servers MUST respond with a 406 Not Acceptable status code if a request’s Accept header contains the JSON API media type and all instances of that media type are modified with media type parameters.
        */
        this.mappings = {
            DrupalToWordpress: {
                // 'nid[0].value': 'id',
                // 'uuid[0].value': 'guid.rendered',
                'title[0].value': 'title.rendered',
                'body[0].value': 'content.rendered',
                'created[0].value': 'date',
                'changed[0].value': 'modified',
                'sticky[0].value': 'sticky',
                'type[0].target_id': 'type',
                'moderation_state[0].target_id': 'status',
                'field_meta_tags[0[.value': 'meta'
                // '_links': '_links',
            }
        };
        if (enforcer !== singletonEnforcer) {
            throw new Error('Cannot construct singleton');
        }
        this._type = 'ConversionService';
        this.yayson = yayson;
    }
    toJsonApi(data, inputType = 'generic', generateId = false) {
        return this.convert(data, inputType, 'jsonapi', generateId);
    }
    fromJsonApi(jsonApiObject) {
        return jsonApiObject.data.attributes;
    }
    convert(input, inputType = 'generic', outputType = 'jsonapi', generateId = false) {
        let output = input;
        if (outputType === 'jsonapi') {
            const Presenter = this.yayson({
                adapter: 'default'
            }).Presenter;
            class ItemsPresenter extends Presenter {
            }
            ;
            ItemsPresenter.prototype['type'] = inputType;
            output = ItemsPresenter['render'](input);
            if (!output.data.id && generateId) {
                output.data.id = Util.getUUID();
            }
            // if(input.type) {
            //     output.type = input.type
            // }
            if (input.meta) {
                output.meta = input.meta;
            }
            // The members data and errors MUST NOT coexist in the same document.
            if (input.errors) {
                output.errors = input.errors;
                delete output.data;
            }
        }
        return output;
    }
    // public migrate(options) {
    //     const self = this;
    //     this.source = options.source;
    //     this.destination = options.destination;
    //     return new Promise(function (resolve, reject) {
    //
    //     });
    // }
    static get instance() {
        if (!this[singleton]) {
            this[singleton] = new ConversionService(singletonEnforcer);
        }
        return this[singleton];
    }
    get type() {
        return this._type;
    }
    set type(value) {
        this._type = value;
    }
}
//# sourceMappingURL=conversion-service.js.map