import { DOCUMENT, CommonModule } from '@angular/common';
import { ɵɵdefineInjectable, ɵɵgetInheritedFactory, ɵsetClassMetadata, Injectable, NgZone, ɵɵdefineNgModule, ɵɵdefineInjector, ɵɵsetNgModuleScope, NgModule } from '@angular/core';
import { ReactiveFormsModule, FormsModule } from '@angular/forms';
import { RouterTestingModule } from '@angular/router/testing';
import { NFC } from '@ionic-native/nfc/ngx';
import { Platform } from '@ionic/angular';
import { ProtocolFactoryService, TAP_INFO_RESOLVER_CONFIG, TAP_CONNECTION_OPTIONS_PROVIDER, TAP_WIFI_SCANNER, TAP_BLE_SCANNER, TAP_NETWORK_SCANNER, TaskManagerUiModule, LibCommonModule } from '@iotize/ionic';
import { DEVICE_AUTH_GUARD_CONFIG } from '@iotize/ionic/auth';
import { defaultInfoResolverConfig } from '@iotize/ionic/config';
import { BehaviorSubject, Subject, of, never } from 'rxjs';
import { __awaiter } from 'tslib';
import { sleep as sleep$1 } from '@iotize/common/utility';
import { Dialogs } from '@ionic-native/dialogs/ngx';
import { AbstractVariable } from '@iotize/tap/data';

class TestingScanner {
    constructor() {
        this.scanning = new BehaviorSubject(false);
        this._results$ = new Subject();
        this._results = [];
    }
    get isScanning() {
        return this.scanning.value;
    }
    get results() {
        return this._results$.asObservable();
    }
    start(option) {
        return __awaiter(this, void 0, void 0, function* () {
            this.clearResults();
            yield sleep$1(100);
            if (!this.isScanning) {
                this.scanning.next(true);
            }
        });
    }
    stop() {
        return __awaiter(this, void 0, void 0, function* () {
            yield sleep$1(100);
            this.scanning.next(false);
        });
    }
    addResult(result) {
        this._results.push(result);
        this._results$.next(this._results);
    }
    clearResults() {
        this._results = [];
        this._results$.next(this._results);
    }
}

class BLEScannerTestingService extends TestingScanner {
}
/** @nocollapse */ BLEScannerTestingService.ɵfac = function BLEScannerTestingService_Factory(t) { return ɵBLEScannerTestingService_BaseFactory(t || BLEScannerTestingService); };
/** @nocollapse */ BLEScannerTestingService.ɵprov = ɵɵdefineInjectable({ token: BLEScannerTestingService, factory: BLEScannerTestingService.ɵfac });
const ɵBLEScannerTestingService_BaseFactory = /*@__PURE__*/ ɵɵgetInheritedFactory(BLEScannerTestingService);
(function () { (typeof ngDevMode === "undefined" || ngDevMode) && ɵsetClassMetadata(BLEScannerTestingService, [{
        type: Injectable
    }], null, null); })();

class ProtocolFactoryTestingService {
    constructor() { }
    create(meta) {
        throw new Error(`Unsupported protocol type "${meta.type}"`);
    }
    isValid(meta) {
        return false;
    }
}
/** @nocollapse */ ProtocolFactoryTestingService.ɵfac = function ProtocolFactoryTestingService_Factory(t) { return new (t || ProtocolFactoryTestingService)(); };
/** @nocollapse */ ProtocolFactoryTestingService.ɵprov = ɵɵdefineInjectable({ token: ProtocolFactoryTestingService, factory: ProtocolFactoryTestingService.ɵfac });
(function () { (typeof ngDevMode === "undefined" || ngDevMode) && ɵsetClassMetadata(ProtocolFactoryTestingService, [{
        type: Injectable
    }], function () { return []; }, null); })();

class ScannerNotAvailable {
    constructor(message) {
        this.message = message;
        this.scanning = of(false);
        this.isScanning = false;
        this.results = never();
    }
    start(option) {
        return Promise.reject(this.message);
    }
    stop() {
        return Promise.reject(this.message);
    }
}
const ProtocolFactoryTestingServiceProvider = {
    provide: ProtocolFactoryService,
    useClass: ProtocolFactoryTestingService,
};
function scannerNotAvailableFactory(name) {
    const factory = () => {
        const scanner = new ScannerNotAvailable(`${name} scanner is not available for this test`);
        return scanner;
    };
    return factory;
}
class IotizeIonicTestingModule {
    static forRoot(options) {
        // const providers: Provider[] = [];
        const overwritesProvides = [];
        if (options === null || options === void 0 ? void 0 : options.platform) {
            overwritesProvides.push({
                provide: Platform,
                useFactory: (doc, ngZone) => {
                    return new TestingPlatform(doc, ngZone, options === null || options === void 0 ? void 0 : options.platform);
                },
                deps: [DOCUMENT, NgZone],
            });
        }
        return {
            ngModule: IotizeIonicTestingModule,
            providers: [
                // ...providers,
                ...overwritesProvides,
            ],
        };
    }
}
/** @nocollapse */ IotizeIonicTestingModule.ɵfac = function IotizeIonicTestingModule_Factory(t) { return new (t || IotizeIonicTestingModule)(); };
/** @nocollapse */ IotizeIonicTestingModule.ɵmod = ɵɵdefineNgModule({ type: IotizeIonicTestingModule });
/** @nocollapse */ IotizeIonicTestingModule.ɵinj = ɵɵdefineInjector({ providers: [
        BLEScannerTestingService,
        ProtocolFactoryTestingServiceProvider,
        Dialogs,
        {
            provide: TAP_INFO_RESOLVER_CONFIG,
            useValue: defaultInfoResolverConfig,
        },
        NFC,
        {
            provide: DEVICE_AUTH_GUARD_CONFIG,
            useValue: {
                loginUrl: '/device/login',
            },
        },
        {
            provide: TAP_CONNECTION_OPTIONS_PROVIDER,
            useValue: {
                encryption: undefined,
                nfcPairing: false,
                refreshSessionState: true,
                switchProtocol: false,
            },
        },
        {
            provide: TAP_WIFI_SCANNER,
            useFactory: scannerNotAvailableFactory('Wi-Fi'),
        },
        {
            provide: TAP_BLE_SCANNER,
            useClass: BLEScannerTestingService,
        },
        {
            provide: TAP_NETWORK_SCANNER,
            useFactory: scannerNotAvailableFactory('Network'),
        },
    ], imports: [[], CommonModule,
        ReactiveFormsModule,
        FormsModule,
        RouterTestingModule,
        TaskManagerUiModule,
        LibCommonModule] });
(function () { (typeof ngJitMode === "undefined" || ngJitMode) && ɵɵsetNgModuleScope(IotizeIonicTestingModule, { exports: [CommonModule,
        ReactiveFormsModule,
        FormsModule,
        RouterTestingModule,
        TaskManagerUiModule,
        LibCommonModule] }); })();
(function () { (typeof ngDevMode === "undefined" || ngDevMode) && ɵsetClassMetadata(IotizeIonicTestingModule, [{
        type: NgModule,
        args: [{
                declarations: [],
                providers: [
                    BLEScannerTestingService,
                    ProtocolFactoryTestingServiceProvider,
                    Dialogs,
                    {
                        provide: TAP_INFO_RESOLVER_CONFIG,
                        useValue: defaultInfoResolverConfig,
                    },
                    NFC,
                    {
                        provide: DEVICE_AUTH_GUARD_CONFIG,
                        useValue: {
                            loginUrl: '/device/login',
                        },
                    },
                    {
                        provide: TAP_CONNECTION_OPTIONS_PROVIDER,
                        useValue: {
                            encryption: undefined,
                            nfcPairing: false,
                            refreshSessionState: true,
                            switchProtocol: false,
                        },
                    },
                    {
                        provide: TAP_WIFI_SCANNER,
                        useFactory: scannerNotAvailableFactory('Wi-Fi'),
                    },
                    {
                        provide: TAP_BLE_SCANNER,
                        useClass: BLEScannerTestingService,
                    },
                    {
                        provide: TAP_NETWORK_SCANNER,
                        useFactory: scannerNotAvailableFactory('Network'),
                    },
                ],
                imports: [],
                exports: [
                    CommonModule,
                    ReactiveFormsModule,
                    FormsModule,
                    RouterTestingModule,
                    TaskManagerUiModule,
                    LibCommonModule,
                ],
            }]
    }], null, null); })();
class TestingPlatform extends Platform {
    constructor(doc, zone, platformName) {
        super(doc, zone);
        this.platformName = platformName;
    }
    is(platformName) {
        return platformName === this.platformName;
    }
}

class WifiScannerTestingService extends TestingScanner {
}

function sleep(ms) {
    return __awaiter(this, void 0, void 0, function* () {
        return new Promise((resolve) => setTimeout((value) => resolve(value), ms));
    });
}
class VariableInteractionTesting extends AbstractVariable {
    constructor(options) {
        super();
        this.options = options;
        this._rawValue = Uint8Array.from([0]);
    }
    get id() {
        var _a;
        return ((_a = this.options) === null || _a === void 0 ? void 0 : _a.identifier) || 'Unamed variable';
    }
    readRawInner() {
        return __awaiter(this, void 0, void 0, function* () {
            return this._rawValue;
        });
    }
    writeRaw(value) {
        return __awaiter(this, void 0, void 0, function* () {
            this._rawValue = value;
        });
    }
}

/**
 * Generated bundle index. Do not edit.
 */

export { BLEScannerTestingService, IotizeIonicTestingModule, ProtocolFactoryTestingService, ProtocolFactoryTestingServiceProvider, VariableInteractionTesting, WifiScannerTestingService, scannerNotAvailableFactory, sleep };
//# sourceMappingURL=iotize-ionic-testing.js.map
