'use strict';
import { crudCreate, crudUpdate } from '../../meta/server/basic-crud/cruds.js';
import { isNullOrUndefined } from '../../../fd-toolbox/functions/value-checking-functions.js';
import { resourceNames } from '../../../fd-toolbox/resources/resource-names.js';
import { getNewUuid } from '../../../fd-toolbox/uuids.js';
import { createBaseResourceProperties } from '../utils/base-resource-properties.js';
import { createError } from '../../../fd-toolbox/errors/errors.js';
import { metaErrorMessages } from '../../meta/constants/meta-error-messages-constants.js';
import { getOneFeatureByUserId, getFeatureByFeatureCode } from './feature-db.js';
import { toolboxUserConstants } from '../../meta/constants/toolbox-users.js';
import { userFeatureStates, featureStates } from '../../meta/core/resources/enums/feature-enums.js';

var __defProp=Object['defineProperty'],__name=(_0x40ed69,_0x4d79e6)=>__defProp(_0x40ed69,'name',{'value':_0x4d79e6,'configurable':!![]});async function isFeatureEnabled(_0x38fbc0,_0x11ad8a){const _0x4cfc0b=await getOneFeatureByUserId(_0x11ad8a,_0x38fbc0);return !isNullOrUndefined(_0x4cfc0b);}__name(isFeatureEnabled,'isFeatureEnabled');async function switchFeature(_0x3b2084,_0x1a8661,_0x3c5035){const _0x5437e6=await getOneFeatureByUserId(_0x1a8661,_0x3b2084);if(!_0x5437e6){const _0x12f3f5=await getFeatureByFeatureCode(_0x3b2084);if(!_0x12f3f5)throw createError(metaErrorMessages['featureNotFound']['replace']('{0}',_0x3b2084));const _0x38a069={...createBaseResourceProperties(),'userId':_0x1a8661,'state':_0x3c5035?userFeatureStates['enabled']:userFeatureStates['disabled'],'featureId':_0x12f3f5['id']},_0x5432d8={'resourceName':resourceNames['feature'],'resource':_0x38a069,'userId':toolboxUserConstants['workerBotId']};await crudCreate(_0x5432d8);}else {_0x5437e6['state']=_0x3c5035?userFeatureStates['enabled']:userFeatureStates['disabled'];const _0x48b387={'resourceName':resourceNames['feature'],'resource':_0x5437e6,'userId':toolboxUserConstants['workerBotId']};await crudUpdate(_0x48b387);}}__name(switchFeature,'switchFeature');async function createFeature(_0x3af004){const _0x5b17b7={...createBaseResourceProperties(getNewUuid()),'name':_0x3af004,'code':_0x3af004,'state':featureStates['enabled'],'enabledByDefaultForUsers':![]},_0x51994b={'resourceName':resourceNames['feature'],'resource':_0x5b17b7,'userId':toolboxUserConstants['workerBotId']},_0x2d74f5=await crudCreate(_0x51994b);return _0x2d74f5['result'];}__name(createFeature,'createFeature');

export { createFeature, isFeatureEnabled, switchFeature };
