'use strict';

Object.defineProperty(exports, '__esModule', { value: true });

var engine = require('@lwc/engine');
var wireService = require('@lwc/wire-service');

/*
 * Copyright (c) 2018, salesforce.com, inc.
 * All rights reserved.
 * SPDX-License-Identifier: MIT
 * For full license text, see the LICENSE file in the repo root or https://opensource.org/licenses/MIT
 */
function createAdapter(adapterId) {
    let done = false;
    let lastConfig;
    const wiredEventTargets = [];

    const emit = (value) => {
        if (!done) {
            wiredEventTargets.forEach(wiredEventTarget => wiredEventTarget.dispatchEvent(new wireService.ValueChangedEvent({ data: value, error: undefined })));
        }
    };

    const error = (body, status, statusText) => {
        if (!done) {
            done = true;

            if (status && (status < 400 || status > 599)) {
                throw new Error("'status' must be >= 400 or <= 599");
            }

            body = body || [{
                errorCode: 'NOT_FOUND',
                message: 'The requested resource does not exist',
            }];

            status = status || 404;

            statusText = statusText || 'NOT_FOUND';

            const err = {
                body,
                ok: false,
                status,
                statusText,
            };

            wiredEventTargets.forEach(wiredEventTarget => wiredEventTarget.dispatchEvent(new wireService.ValueChangedEvent({ data: undefined, error: err })));
        }
    };

    const getLastConfig = () => {
        return lastConfig;
    };

    const add = (arr, item) => {
        const idx = arr.indexOf(item);
        if (idx === -1) {
            arr.push(item);
        }
    };

    wireService.register(adapterId, (wiredEventTarget) => {
        done = false;
        add(wiredEventTargets, wiredEventTarget);
        wiredEventTarget.dispatchEvent(new wireService.ValueChangedEvent({ data: undefined, error: undefined }));

        wiredEventTarget.addEventListener('connect', () => {
            done = false;
            lastConfig = {};
            add(wiredEventTargets, wiredEventTarget);
        });

        wiredEventTarget.addEventListener('disconnect', () => {
            done = true;
            lastConfig = undefined;
            const idx = wiredEventTargets.indexOf(wiredEventTarget);
            if (idx > -1) {
                wiredEventTargets.splice(idx, 1);
            }
        });

        wiredEventTarget.addEventListener('config', (newConfig) => {
            lastConfig = newConfig;
        });
    });

    return {
        emit,
        error,
        getLastConfig
    };
}

/*
 * Copyright (c) 2018, salesforce.com, inc.
 * All rights reserved.
 * SPDX-License-Identifier: MIT
 * For full license text, see the LICENSE file in the repo root or https://opensource.org/licenses/MIT
 */
function createAdapter$1(adapterId) {
    let lastConfig;
    const wiredEventTargets = [];

    const emit = (value) => {
        wiredEventTargets.forEach(wiredEventTarget => wiredEventTarget.dispatchEvent(new wireService.ValueChangedEvent(value)));
    };

    const getLastConfig = () => {
        return lastConfig;
    };

    const add = (arr, item) => {
        const idx = arr.indexOf(item);
        if (idx === -1) {
            arr.push(item);
        }
    };

    wireService.register(adapterId, (wiredEventTarget) => {
        add(wiredEventTargets, wiredEventTarget);

        wiredEventTarget.addEventListener('connect', () => {
            lastConfig = {};
            add(wiredEventTargets, wiredEventTarget);
        });

        wiredEventTarget.addEventListener('disconnect', () => {
            lastConfig = undefined;
            const idx = wiredEventTargets.indexOf(wiredEventTarget);
            if (idx > -1) {
                wiredEventTargets.splice(idx, 1);
            }
        });

        wiredEventTarget.addEventListener('config', (newConfig) => {
            lastConfig = newConfig;
        });
    });

    return {
        emit,
        getLastConfig
    };
}

/*
 * Copyright (c) 2018, salesforce.com, inc.
 * All rights reserved.
 * SPDX-License-Identifier: MIT
 * For full license text, see the LICENSE file in the repo root or https://opensource.org/licenses/MIT
 */
function createAdapter$2(adapterId) {
    let done = false;
    let lastConfig;
    const wiredEventTargets = [];

    const emit = (value) => {
        if (!done) {
            wiredEventTargets.forEach(wiredEventTarget => wiredEventTarget.dispatchEvent(new wireService.ValueChangedEvent({ data: value, error: undefined })));
        }
    };

    const error = (body, status, statusText) => {
        if (!done) {
            done = true;

            if (status && (status < 400 || status > 599)) {
                throw new Error("'status' must be >= 400 or <= 599");
            }

            body = body || {
                message: 'An internal server error has occurred',
            };

            status = status || 400;

            statusText = statusText || 'Bad Request';

            const err = {
                body,
                ok: false,
                status,
                statusText,
            };

            wiredEventTargets.forEach(wiredEventTarget => wiredEventTarget.dispatchEvent(new wireService.ValueChangedEvent({ data: undefined, error: err })));
        }
    };

    const getLastConfig = () => {
        return lastConfig;
    };

    const add = (arr, item) => {
        const idx = arr.indexOf(item);
        if (idx === -1) {
            arr.push(item);
        }
    };

    wireService.register(adapterId, (wiredEventTarget) => {
        done = false;
        add(wiredEventTargets, wiredEventTarget);
        wiredEventTarget.dispatchEvent(new wireService.ValueChangedEvent({ data: undefined, error: undefined }));

        wiredEventTarget.addEventListener('connect', () => {
            done = false;
            lastConfig = {};
            add(wiredEventTargets, wiredEventTarget);
        });

        wiredEventTarget.addEventListener('disconnect', () => {
            done = true;
            lastConfig = undefined;
            const idx = wiredEventTargets.indexOf(wiredEventTarget);
            if (idx > -1) {
                wiredEventTargets.splice(idx, 1);
            }
        });

        wiredEventTarget.addEventListener('config', (newConfig) => {
            lastConfig = newConfig;
        });
    });

    return {
        emit,
        error,
        getLastConfig
    };
}

/*
 * Copyright (c) 2018, salesforce.com, inc.
 * All rights reserved.
 * SPDX-License-Identifier: MIT
 * For full license text, see the LICENSE file in the repo root or https://opensource.org/licenses/MIT
 */
let registered = false;

// will not be necessary once wire-service is self-registering (W-4844671)
function ensureWireServiceRegistered() {
    if (!registered) {
        wireService.registerWireService(engine.register);
        registered = true;
    }
}

function registerLdsTestWireAdapter(identifier) {
    if (!identifier) {
        throw new Error('No adapter specified');
    }

    ensureWireServiceRegistered();

    return createAdapter(identifier);
}

function registerApexTestWireAdapter(identifier) {
    if (!identifier) {
        throw new Error('No adapter specified');
    }

    ensureWireServiceRegistered();

    return createAdapter$2(identifier);
}

function registerTestWireAdapter(identifier) {
    if (!identifier) {
        throw new Error('No adapter specified');
    }

    ensureWireServiceRegistered();

    return createAdapter$1(identifier);
}

exports.registerLdsTestWireAdapter = registerLdsTestWireAdapter;
exports.registerApexTestWireAdapter = registerApexTestWireAdapter;
exports.registerTestWireAdapter = registerTestWireAdapter;
