import Model , { IAction } from './' ; import Config from '../config' ; import Errors from '../errors' ; import Manager from '../manager' ; import { generateUniqModelKey } from '../lib' ; type TOptionFunc = ( ( ) => IAction ) | null ; export interface IUXOptions { key ? : string ; connected : boolean ; } export interface IOptions { key ? : string ; connected ? : boolean ; save ? : TOptionFunc ; store ? : TOptionFunc ; nodeModel ? : Model | null ; collectionModel ? : Model | null ; } export default class OptionManager { private _m : Model ; private _isKeyGenerated : boolean = false ; private _options : IOptions = { key : '' , connected : false , save : null , store : null , nodeModel : null , collectionModel : null } ; constructor ( m , option ) { this . _m = m ; this . _init ( option ) ; } private _init = ( options ) => { this . _set ( options ) ; const key = this . key ( ) ; if ( this . isConnected ( ) ) { if ( Config . isNextJS ( ) && ! key ) throw Errors . uniqKeyRequiredOnNextJS ( ) ; if ( key && Manager . models ( ) . exist ( key ) && ! Config . isNextJSServer ( ) ) throw Errors . keyAlreadyExist ( key ) ; if ( ! key ) { this . _setKey ( generateUniqModelKey ( this . _model ( ) ) ) ; this . _setKeyAsGenerated ( ) ; } } return this ; } ; private _setKey = ( key ) => this . _set ( { key } ) ; private _setKeyAsGenerated = ( ) => this . _isKeyGenerated = true ; private _model = ( ) : Model => this . _m ; private _set = ( o ) => this . _options = Object . assign ( { } , this . _options , o ) ; public isKeyGenerated = ( ) : boolean => this . _isKeyGenerated ; public get = ( ) : IOptions => this . _options ; public key = ( ) : string => this . get ( ) . key as string ; public isConnected = ( ) : boolean => this . get ( ) . connected as boolean ; public nodeModel = ( ) => this . get ( ) . nodeModel ; public collectionModel = ( ) => this . get ( ) . collectionModel ; public kids = ( ) => { return { save : this . _model ( ) . action ( ) . save , store : this . _model ( ) . action ( ) . store , } ; } ; }