@univerjs-pro/bases 1.5.2 → 1.6.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/lib/cjs/config-2_bLW0kY.js +2 -0
- package/lib/cjs/index.js +1 -1
- package/lib/cjs/worker.js +1 -1
- package/lib/config-DtzarxLi.js +2 -0
- package/lib/es/config-DtzarxLi.js +2 -0
- package/lib/es/index.js +1 -1
- package/lib/es/worker.js +1 -1
- package/lib/index.js +1 -1
- package/lib/types/commands/commands/add-gantt-predecessor.command.d.ts +12 -0
- package/lib/types/commands/commands/set-base-record-values.command.d.ts +7 -0
- package/lib/types/commands/mutations/apply-base-json1.mutation.d.ts +2 -0
- package/lib/types/fields/progress-format.d.ts +5 -0
- package/lib/types/index.d.ts +8 -1
- package/lib/types/models/base-hierarchy.d.ts +1 -1
- package/lib/types/ot/json1-op-factory.d.ts +6 -1
- package/lib/types/services/base-projection.service.d.ts +5 -3
- package/lib/types/services/base-sync-status.service.d.ts +7 -0
- package/lib/types/views/gantt-view.d.ts +36 -0
- package/lib/umd/index.js +2 -2
- package/lib/worker.js +1 -1
- package/package.json +11 -10
- package/lib/cjs/config-DfX5nPik.js +0 -2
- package/lib/config-DFPOsWdz.js +0 -2
- package/lib/es/config-DFPOsWdz.js +0 -2
package/lib/es/index.js
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
import{$ as e,A as t,B as n,C as r,D as i,E as a,F as o,G as s,H as c,I as l,J as u,K as d,L as f,M as p,N as m,O as h,P as g,Q as _,R as v,S as y,T as b,U as x,V as S,W as ee,X as C,Y as w,Z as T,_ as te,a as ne,at as re,b as ie,c as ae,ct as E,d as oe,et as se,f as D,g as O,h as ce,it as le,j as ue,k,l as de,m as fe,nt as pe,o as me,ot as he,p as A,q as ge,r as _e,rt as ve,s as j,st as ye,t as be,tt as xe,u as Se,v as Ce,w as we,x as Te,y as Ee,z as De}from"./config-DFPOsWdz.js";import{BASE_RECORD_ID_FIELD_ID as M,BaseConditionalColorOperator as Oe,BaseConditionalColorTarget as ke,BaseConditionalDateMode as Ae,BaseDataModel as je,BaseFieldType as N,BaseHierarchyInvalidReason as Me,BaseRecordLinkRole as P,BaseViewType as Ne,CellValueType as Pe,ColorKit as Fe,CommandType as F,CustomCommandExecutionError as Ie,DependentOn as Le,Disposable as Re,ErrorService as ze,ICommandService as I,IConfigService as Be,IPermissionService as Ve,IResourceManagerService as He,IUndoRedoService as L,IUniverInstanceService as R,Inject as Ue,Injector as We,JSON1 as z,JSONX as Ge,ObjectMatrix as Ke,ObjectPermissionRuleModel as qe,ObjectPermissionService as Je,Optional as Ye,PermissionStatus as B,Plugin as Xe,Tools as V,UniverInstanceType as H,allocateBaseFormulaTableName as Ze,assertBaseTableRecordIdentity as Qe,createBaseFormulaTableNameMap as $e,createBaseRecordIdField as et,createIdentifier as tt,generateRandomId as nt,getBaseFormulaTableName as rt,isBaseRecordIdFieldName as it,isValidBaseRecordId as at,merge as ot,nameCharacterCheck as st,numfmt as ct,regexp as U,sequenceExecute as lt,toDisposable as ut,touchDependencies as dt}from"@univerjs/core";import{FormulaCalculationTriggerService as ft,IActiveDirtyManagerService as pt,RemoveSuperTableMutation as mt,SetFormulaCalculationResultMutation as ht,SetSuperTableMutation as gt,SetTriggerFormulaCalculationStartMutation as _t,UniverProFormulaEnginePlugin as vt,refactorFormulaUnitQualifier as yt}from"@univerjs-pro/engine-formula";import{UnitAction as W,UnitObject as G}from"@univerjs/protocol";import{UniverLicensePlugin as bt}from"@univerjs-pro/license";import{DataSyncPrimaryController as xt}from"@univerjs/rpc";import{FormulaCalculationSessionService as St,FormulaResultApplicationType as Ct}from"@univerjs/engine-formula";const K={table:e=>[`tables`,e],field:(e,t)=>[`tables`,e,`fields`,t],fieldOrder:(e,t)=>[`tables`,e,`fieldOrder`,t],record:(e,t)=>[`tables`,e,`records`,t],cell:(e,t,n)=>[`tables`,e,`records`,t,`values`,n],cellData:(e,t,n)=>[`tables`,e,`cellData`,String(t),String(n)],attachmentSet:(e,t)=>[`tables`,e,`resources`,`attachmentSets`,t],attachment:(e,t)=>[`tables`,e,`resources`,`attachments`,t],view:(e,t)=>[`tables`,e,`views`,t],viewFieldHidden:(e,t,n)=>[`tables`,e,`views`,t,`fieldSettings`,n,`hidden`]};var q=class{setBaseName(e,t){let n=this.renameFormulaUnitReferences(e,e.name,t);return h([this._replaceOptional([`name`],e.name,t),...n?[n]:[]])}renameFormulaUnitReferences(e,t,n){let r=Object.values(e.tables).flatMap(e=>Object.values(e.fields).flatMap(r=>{var i,a;if(r.type!==N.Formula)return[];let o=String(((i=r.config)==null?void 0:i.formula)??``),s=yt(o,t,n);return s===o?[]:[this._replaceOptional([...K.field(e.id,r.id),`config`,`formula`],(a=r.config)==null?void 0:a.formula,s)]}));return r.length>0?h(r):void 0}createTable(e,t,n=e.tableOrder.length){let r=Ze(t.name,$e(e).values(),t.formulaName),i=t.formulaName===r?t:{...t,formulaName:r};return h([...this._materializeFormulaTableNames(e),z.insertOp(K.table(t.id),i),z.insertOp([`tableOrder`,n],t.id)])}renameTable(e,t,n){let r=e.tables[t];return h([...this._materializeFormulaTableNames(e),z.replaceOp([...K.table(t),`name`],r.name,n)])}deleteTable(e,t){let n=e.tables[t],r=e.tableOrder.indexOf(t);return h([...this._materializeFormulaTableNames(e,/* @__PURE__ */ new Set([t])),z.removeOp(K.table(t),n),z.removeOp([`tableOrder`,r],t)])}createRecord(e,t,n){return this.createRecords(e,t,[n])}createRecords(e,t,n){let r=e.tables[t],i=n.filter(e=>e&&!r.records[e.id]).map(e=>({...e,values:{...e.values,[M]:e.id}}));if(!i.length)return z.replaceOp([...K.table(t),`records`],r.records,r.records);let a=new z.WriteCursor,o=[...i].sort((e,t)=>e.id<t.id?-1:+(e.id>t.id)),s=[...K.table(t),`records`];a.descendPath(s);for(let e of o)a.descend(e.id),a.write(`i`,e),a.ascend();for(let e=0;e<s.length;e+=1)a.ascend();return h([a.get(),this._createRecordOrderOp(e,t,i)])}deleteRecord(e,t,n){return this.deleteRecords(e,t,[n])}deleteRecords(e,t,n){let r=e.tables[t],i=Array.from(new Set(n)).filter(e=>r.records[e]);if(!i.length)return z.replaceOp([...K.table(t),`records`],r.records,r.records);let a=new Set(i),o=ge(r,i).map(n=>this.updateCell(e,t,n.recordId,n.fieldId,``));return h([this._deleteRecordsBaseOp(e,t,i,a),...o])}createDeleteRecordsUndoOp(e,t,n,r){let a=e.tables[n],o=Array.from(new Set(r)).filter(e=>a.records[e]),s=new Set(o),c=new i().invertOp(e,this._deleteRecordsBaseOp(e,n,o,s)),l=ge(a,o).map(e=>this.updateCell(t,n,e.recordId,e.fieldId,e.oldParentRecordId??``));return h([c,...l])}_deleteRecordsBaseOp(e,t,n,r){let i=e.tables[t],a=n.map(e=>{var t;return{recordId:e,index:((t=i.recordOrder)==null?void 0:t.indexOf(e))??-1}}).filter(e=>e.index>=0).sort((e,t)=>t.index-e.index);return h([...n.map(e=>z.removeOp(K.record(t,e),i.records[e])),...a.map(({recordId:e,index:n})=>z.removeOp([`tables`,t,`recordOrder`,n],e)),...this._deleteAttachmentResources(i,t,e=>{let t=e.slice(e.lastIndexOf(``)+1);return r.has(t)})])}duplicateRecord(e,t,n,r){return this.createRecord(e,t,{...r,values:{...e.tables[t].records[n].values,...r.values,[M]:r.id}})}updateRecordOrder(e,t,n,r){let i=e.tables[t],a=[...K.record(t,n),`orderKey`],o=Object.values(i.records).map(e=>e.id===n?{...e,orderKey:r}:e).sort((e,t)=>String(e.orderKey??e.id).localeCompare(String(t.orderKey??t.id))).map(e=>e.id),s=[...K.table(t),`recordOrder`];return h([this._replaceOptional(a,i.records[n].orderKey,r),i.recordOrder?z.replaceOp(s,i.recordOrder,o):z.insertOp(s,o)])}updateCell(e,t,n,r,i){var a,o;let s=e.tables[t],c=kt(s,n,r)?s:k(s),l=(a=c.rowIndex)==null?void 0:a[n],u=(o=c.colIndex)==null?void 0:o[r];if(l==null||u==null){var d;return this._replaceOptional(K.cell(t,n,r),(d=e.tables[t])==null||(d=d.records[n])==null?void 0:d.values[r],i)}return this.setRangeValues(e,t,[{row:l,col:u,value:i}])}setRecordValues(e,t,n){let r=e.tables[t],i=n.every(e=>Object.keys(e.values).every(t=>kt(r,e.recordId,t)))?r:k(r),a=n.flatMap(e=>Object.entries(e.values).flatMap(([t,n])=>{var r,a;let o=(r=i.rowIndex)==null?void 0:r[e.recordId],s=(a=i.colIndex)==null?void 0:a[t];return o==null||s==null?[]:[{row:o,col:s,value:n}]})),o=n.flatMap(n=>Object.entries(n.recordPatch??{}).map(([r,i])=>{var a;let o=[...K.record(t,n.recordId),r],s=(a=e.tables[t])==null||(a=a.records[n.recordId])==null?void 0:a[r];return this._replaceOptional(o,s,i)}));return h([this.setRangeValues(e,t,a),...o])}setRangeValues(e,n,r){let i=e.tables[n],a=r.every(e=>At(i,e.row,e.col))?i:k(i),s=/* @__PURE__ */ new Map,c=r.flatMap(e=>{var r,i,c,u;let d=(r=a.colId)==null?void 0:r[e.col],p=(i=a.rowId)==null?void 0:i[e.row],m=d?a.fields[d]:void 0;if(d===M||(m==null?void 0:m.type)===N.RecordId)throw Error(`[BaseJson1OpFactory]: cannot write record-id.`);let h=l(e.value,m),g=(c=a.cellData)==null||(c=c[e.row])==null?void 0:c[e.col],_=this._replaceOptional(K.cellData(n,e.row,e.col),g,h);if(!d||!p)return[_];let v=(u=a.records[p])==null?void 0:u.values[d],y=(m==null?void 0:m.type)===N.Attachment?o(e.value):f(e.value,h,m,a,p),b=[_,this._replaceOptional(K.cell(n,p,d),v,y)];return(m==null?void 0:m.type)===N.Attachment&&s.set(t(d,p),{recordId:p,fieldId:d,attachments:y}),b});return c.length?h([...c,...this._setAttachmentResources(a,n,Array.from(s.values()))]):z.replaceOp([`tables`,n,`cellData`],a.cellData,a.cellData)}createField(e,t,n){let r=e.tables[t];if(r.fields[n.field.id]||r.fieldOrder.includes(n.field.id))return z.replaceOp([`tables`,t,`fieldOrder`],r.fieldOrder,r.fieldOrder);let i=Math.max(1,n.index??r.fieldOrder.length),a=Object.values(r.views??{}).flatMap(e=>{var r;if(!e.fieldOrder||e.fieldOrder.includes(n.field.id))return[];let a=Pt(((r=n.viewIndexes)==null?void 0:r[e.id])??i,e.fieldOrder.length);return[z.insertOp([...K.view(t,e.id),`fieldOrder`,a],n.field.id)]});return h([...n.field.type===N.Formula?this._materializeFormulaTableNames(e):[],z.insertOp(K.field(t,n.field.id),n.field),z.insertOp(K.fieldOrder(t,i),n.field.id),...a])}duplicateField(e,t,n,r){let i=e.tables[t];if(!i.fields[n]||i.fields[r.field.id]||i.fieldOrder.includes(r.field.id))return z.replaceOp([`tables`,t,`fieldOrder`],i.fieldOrder,i.fieldOrder);let a=r.index??i.fieldOrder.indexOf(n)+1,o=i.fieldOrder.filter(e=>e!==r.field.id),s=Mt(o,r.field.id,a),c=Object.values(i.views??{}).flatMap(e=>{var i,s,c,l;let u=(i=e.fieldOrder)!=null&&i.length?e.fieldOrder:o;if(u.includes(r.field.id))return[];let d=Mt(u,r.field.id,((s=r.viewIndexes)==null?void 0:s[e.id])??a),f=(c=e.fieldSettings)==null?void 0:c[n];return[this._replaceOptional([...K.view(t,e.id),`fieldOrder`],e.fieldOrder,d),...f?[this._replaceOptional([...K.view(t,e.id),`fieldSettings`,r.field.id],(l=e.fieldSettings)==null?void 0:l[r.field.id],f)]:[]]}),l=Object.values(i.records).filter(e=>Object.prototype.hasOwnProperty.call(e.values,n)).map(e=>this._replaceOptional(K.cell(t,e.id,r.field.id),e.values[r.field.id],e.values[n]));return h([...r.field.type===N.Formula?this._materializeFormulaTableNames(e):[],z.insertOp(K.field(t,r.field.id),r.field),z.replaceOp([`tables`,t,`fieldOrder`],i.fieldOrder,s),...c,...l])}updateField(e,t,n,r){let i=e.tables[t].fields[n],a={...i,...r};Object.keys(r).forEach(e=>{r[e]===void 0&&delete a[e]});let o=wt(e.tables[t],n,i,a);a=o.field;let s=z.replaceOp(K.field(t,n),i,a);return a.type===N.Formula?h([...this._materializeFormulaTableNames(e),s,...this._setFormulaFieldCells(e,t,n,a)]):h([s,...o.patches.length?[this.setRangeValues(e,t,o.patches)]:[]])}changeFieldType(e,t,n,r){return this.updateField(e,t,n,r)}deleteField(e,t,n){let r=e.tables[t];if(!(r!=null&&r.fields[n])||r.primaryFieldId===n||n===M)return z.replaceOp([`tables`,t,`fieldOrder`],r==null?void 0:r.fieldOrder,r==null?void 0:r.fieldOrder);let i=r.fieldOrder.indexOf(n),a=Object.values(r.views).flatMap(e=>{var r,i,a,o,s,c,l;let u=[],d=((r=e.fieldOrder)==null?void 0:r.indexOf(n))??-1;d>=0&&u.push(z.removeOp([...K.view(t,e.id),`fieldOrder`,d],n));let f=(i=e.fieldSettings)==null?void 0:i[n];f!==void 0&&u.push(z.removeOp([...K.view(t,e.id),`fieldSettings`,n],f));let p=e.filter?{...e.filter,conditions:e.filter.conditions.filter(e=>e.fieldId!==n)}:e.filter;p&&p.conditions.length!==((a=e.filter)==null?void 0:a.conditions.length)&&u.push(this._replaceOptional([...K.view(t,e.id),`filter`],e.filter,p));let m=(o=e.sort)==null?void 0:o.filter(e=>e.fieldId!==n);m&&m.length!==((s=e.sort)==null?void 0:s.length)&&u.push(this._replaceOptional([...K.view(t,e.id),`sort`],e.sort,m));let h=(c=e.group)==null?void 0:c.filter(e=>e.fieldId!==n);h&&h.length!==((l=e.group)==null?void 0:l.length)&&u.push(this._replaceOptional([...K.view(t,e.id),`group`],e.group,h));let g=Nt(e.config,n);return JSON.stringify(g)!==JSON.stringify(e.config)&&u.push(z.replaceOp([...K.view(t,e.id),`config`],e.config,g)),u}),o=Object.values(r.records).flatMap(e=>Object.prototype.hasOwnProperty.call(e.values,n)?[z.removeOp(K.cell(t,e.id,n),e.values[n])]:[]);return h([z.removeOp(K.field(t,n),r.fields[n]),z.removeOp(K.fieldOrder(t,i),n),...a,...o,...this._deleteAttachmentResources(r,t,e=>e.startsWith(`${n}`))])}moveField(e,t,n,r){let i=e.tables[t].fieldOrder;if(n===M||r.beforeFieldId===M)throw Error(`[BaseJson1OpFactory]: cannot move a field before record-id.`);let a=jt(i,n,r);return z.replaceOp([`tables`,t,`fieldOrder`],i,a)}createView(e,t,n,r=e.tables[t].viewOrder.length){return h([z.insertOp(K.view(t,n.id),n),z.insertOp([`tables`,t,`viewOrder`,r],n.id)])}renameView(e,t,n,r){return z.replaceOp([...K.view(t,n),`name`],e.tables[t].views[n].name,r)}deleteView(e,t,n){let r=e.tables[t],i=r.views[n],a=r.viewOrder.indexOf(n);return h([z.removeOp(K.view(t,n),i),z.removeOp([`tables`,t,`viewOrder`,a],n)])}moveView(e,t,n,r){let i=e.tables[t].viewOrder,a=jt(i,n,{beforeFieldId:r.beforeViewId,afterFieldId:r.afterViewId});return z.replaceOp([`tables`,t,`viewOrder`],i,a)}updateViewConfig(e,t,n,r){let i=e.tables[t].views[n].config??{},a={...i,...r};return Object.keys(r).forEach(e=>{r[e]===void 0&&delete a[e]}),z.replaceOp([...K.view(t,n),`config`],i,a)}setViewFilter(e,t,n,r){return this._replaceOptional([...K.view(t,n),`filter`],e.tables[t].views[n].filter,r)}setViewSort(e,t,n,r){return this._replaceOptional([...K.view(t,n),`sort`],e.tables[t].views[n].sort,r)}setViewGroup(e,t,n,r){return this._replaceOptional([...K.view(t,n),`group`],e.tables[t].views[n].group,r)}setViewFieldVisible(e,t,n,r,i){return this._setViewFieldSetting(e,t,n,r,{hidden:!i})}setViewFieldWidth(e,t,n,r,i){return this._setViewFieldSetting(e,t,n,r,{width:i})}setViewFieldOrder(e,t,n,r,i){let a=e.tables[t].views[n],o=jt(a.fieldOrder??e.tables[t].fieldOrder,r,i);return this._replaceOptional([...K.view(t,n),`fieldOrder`],a.fieldOrder,o)}_setViewFieldSetting(e,t,n,r,i){let a=e.tables[t].views[n].fieldSettings??{},o={...a,[r]:{...a[r],...i}};return this._replaceOptional([...K.view(t,n),`fieldSettings`],e.tables[t].views[n].fieldSettings,o)}_createRecordOrderOp(e,t,n){let r=e.tables[t],i=new Set(n.map(e=>e.id)),a=[...Object.values(r.records).filter(e=>!i.has(e.id)),...n].sort((e,t)=>String(e.orderKey??e.id).localeCompare(String(t.orderKey??t.id))).map(e=>e.id),o=[...K.table(t),`recordOrder`];return r.recordOrder?z.replaceOp(o,r.recordOrder,a):z.insertOp(o,a)}_materializeFormulaTableNames(e,t=/* @__PURE__ */ new Set){let n=$e(e);return Object.values(e.tables).filter(e=>!t.has(e.id)).sort((e,t)=>e.id.localeCompare(t.id)).flatMap(e=>{let t=n.get(e.id);return!t||e.formulaName===t?[]:[this._replaceOptional([...K.table(e.id),`formulaName`],e.formulaName,t)]})}_setFormulaFieldCells(e,t,n,r){var i,a;let o=k(e.tables[t]),s=String(((i=r.config)==null?void 0:i.formula)??``).trim(),c=(a=o.colIndex)==null?void 0:a[n];if(c==null)return[];let l=o.recordOrder??Object.keys(o.records),u=l.map(e=>{var t;return(t=o.rowIndex)==null?void 0:t[e]}).find(e=>e!=null);return l.flatMap(e=>{var r,i;let a=(r=o.rowIndex)==null?void 0:r[e];if(a==null)return[];let l=(i=o.cellData)==null||(i=i[a])==null?void 0:i[c],d=s?a===u?{f:s,si:n}:{si:n}:{v:null,t:null};return[this._replaceOptional(K.cellData(t,a,c),l,d)]})}_setAttachmentResources(e,n,r){var i;let a=new Map(Object.entries(((i=e.resources)==null?void 0:i.attachmentSets)??{})),o=/* @__PURE__ */ new Map,s=/* @__PURE__ */ new Set,c=r.map(({recordId:r,fieldId:i,attachments:c})=>{var l;let u=t(i,r),d=c.map((e,t)=>String(e.id??`${u}\u001F${t}`));return s.add(u),a.set(u,d),c.forEach((e,t)=>{let n=d[t];o.set(n,{...e,id:n})}),this._replaceOptional(K.attachmentSet(n,u),(l=e.resources)==null||(l=l.attachmentSets)==null?void 0:l[u],d)}),l=Array.from(o).map(([t,r])=>{var i;return this._replaceOptional(K.attachment(n,t),(i=e.resources)==null||(i=i.attachments)==null?void 0:i[t],r)}),u=new Set(Array.from(a.values()).flat()),d=new Set(Array.from(s).flatMap(t=>{var n;return((n=e.resources)==null||(n=n.attachmentSets)==null?void 0:n[t])??[]})),f=Array.from(d).flatMap(t=>{var r;let i=(r=e.resources)==null||(r=r.attachments)==null?void 0:r[t];return i!==void 0&&!u.has(t)?[z.removeOp(K.attachment(n,t),i)]:[]});return[...c,...l,...f]}_deleteAttachmentResources(e,t,n){var r;let i=((r=e.resources)==null?void 0:r.attachmentSets)??{},a=Object.entries(i).filter(([e])=>n(e));if(!a.length)return[];let o=new Set(Object.entries(i).filter(([e])=>!n(e)).flatMap(([,e])=>e)),s=new Set(a.flatMap(([,e])=>e));return[...a.map(([e,n])=>z.removeOp(K.attachmentSet(t,e),n)),...Array.from(s).flatMap(n=>{var r;let i=(r=e.resources)==null||(r=r.attachments)==null?void 0:r[n];return i!==void 0&&!o.has(n)?[z.removeOp(K.attachment(t,n),i)]:[]})]}_replaceOptional(e,t,n){return t===void 0?z.insertOp(e,n):z.replaceOp(e,t,n)}};function wt(e,t,n,r){var i;let a=r.type;if(n.type!==a||a!==N.SingleSelect&&a!==N.MultiSelect||Tt(r.config.optionSource))return{field:r,patches:[]};let o=Et(n),s=Et(r);if(![...o].some(e=>!s.has(e)))return{field:r,patches:[]};let c=Dt(r.defaultValue,s,a),l=Ot(c,r.defaultValue)?r:{...r,defaultValue:c},u=k(e),d=(i=u.colIndex)==null?void 0:i[t];return d==null?{field:l,patches:[]}:{field:l,patches:(u.recordOrder??[]).flatMap(e=>{var n,r;let i=(n=u.rowIndex)==null?void 0:n[e],o=(r=u.records[e])==null?void 0:r.values[t],c=Dt(o,s,a);return i==null||Ot(c,o)?[]:[{row:i,col:d,value:c??null}]})}}function Tt(e){return typeof e==`object`&&!!e&&`type`in e&&e.type===`field`}function Et(e){var t;let n=Array.isArray((t=e.config)==null?void 0:t.options)?e.config.options:[];return new Set(n.flatMap(e=>{if(!e||typeof e!=`object`||!(`id`in e))return[];let t=String(e.id);return t?[t]:[]}))}function Dt(e,t,n){if(e==null||e===``)return e;if(n===N.SingleSelect)return t.has(String(e))?e:null;let r=Array.isArray(e)?e.map(String):String(e).split(`,`).map(e=>e.trim()).filter(Boolean),i=r.filter(e=>t.has(e));return i.length===r.length?e:Array.isArray(e)?i:String(e).includes(`,`)?i.join(`,`):i[0]??[]}function Ot(e,t){return JSON.stringify(e)===JSON.stringify(t)}function kt(e,t,n){var r,i,a,o;let s=(r=e.rowIndex)==null?void 0:r[t],c=(i=e.colIndex)==null?void 0:i[n];return s!=null&&c!=null&&((a=e.rowId)==null?void 0:a[s])===t&&((o=e.colId)==null?void 0:o[c])===n&&e.cellData!=null}function At(e,t,n){var r,i;return((r=e.rowId)==null?void 0:r[t])!=null&&((i=e.colId)==null?void 0:i[n])!=null&&e.cellData!=null}function jt(e,t,n){let r=e.filter(e=>e!==t),i=n.beforeFieldId?r.indexOf(n.beforeFieldId):n.afterFieldId?r.indexOf(n.afterFieldId)+1:r.length;return r.splice(i<0?r.length:i,0,t),r}function Mt(e,t,n){let r=e.filter(e=>e!==t);return r.splice(Pt(n,r.length),0,t),r}function Nt(e,t,n){if(Array.isArray(e))return n===`fieldIds`?e.filter(e=>e!==t):e.flatMap(e=>{if(e&&typeof e==`object`&&!Array.isArray(e)&&e.fieldId===t)return[];let n=Nt(e,t);return n===void 0?[]:[n]});if(!e||typeof e!=`object`)return n!=null&&n.endsWith(`FieldId`)&&e===t?void 0:e;let r=e;if(r.fieldId!==t)return Object.fromEntries(Object.entries(r).flatMap(([e,r])=>{if(e===t&&n===`fieldSettings`)return[];let i=Nt(r,t,e);return i===void 0?[]:[[e,i]]}))}function Pt(e,t){return Math.max(0,Math.min(e,t))}var Ft=class{constructor(e){this.unitId=e,O(this,`type`,G.Base),O(this,`subType`,W.Comment),O(this,`status`,B.INIT),O(this,`id`,void 0),O(this,`value`,!0),this.id=`${this.type}.${this.subType}_${e}`}},It=class{constructor(e){this.unitId=e,O(this,`type`,G.Base),O(this,`subType`,W.Copy),O(this,`status`,B.INIT),O(this,`id`,void 0),O(this,`value`,!0),this.id=`${this.type}.${this.subType}_${e}`}},Lt=class{constructor(e){this.unitId=e,O(this,`type`,G.Base),O(this,`subType`,W.Edit),O(this,`status`,B.INIT),O(this,`id`,void 0),O(this,`value`,!0),this.id=`${this.type}.${this.subType}_${e}`}},Rt=class{constructor(e){this.unitId=e,O(this,`type`,G.Base),O(this,`subType`,W.Export),O(this,`status`,B.INIT),O(this,`id`,void 0),O(this,`value`,!0),this.id=`${this.type}.${this.subType}_${e}`}},zt=class{constructor(e,t){this.unitId=e,this.objectId=t,O(this,`type`,G.BaseDashboard),O(this,`subType`,W.Edit),O(this,`status`,B.INIT),O(this,`id`,void 0),O(this,`value`,!0),this.id=`${this.type}.${this.subType}_${e}_${t}`}},Bt=class{constructor(e,t){this.unitId=e,this.objectId=t,O(this,`type`,G.BaseField),O(this,`subType`,W.Edit),O(this,`status`,B.INIT),O(this,`id`,void 0),O(this,`value`,!0),this.id=`${this.type}.${this.subType}_${e}_${t}`}},Vt=class{constructor(e,t){this.unitId=e,this.objectId=t,O(this,`type`,G.BaseRecord),O(this,`subType`,W.Edit),O(this,`status`,B.INIT),O(this,`id`,void 0),O(this,`value`,!0),this.id=`${this.type}.${this.subType}_${e}_${t}`}},Ht=class{constructor(e,t){this.unitId=e,this.objectId=t,O(this,`type`,G.BaseTable),O(this,`subType`,W.Edit),O(this,`status`,B.INIT),O(this,`id`,void 0),O(this,`value`,!0),this.id=`${this.type}.${this.subType}_${e}_${t}`}},Ut=class{constructor(e,t){this.unitId=e,this.objectId=t,O(this,`type`,G.BaseView),O(this,`subType`,W.Edit),O(this,`status`,B.INIT),O(this,`id`,void 0),O(this,`value`,!0),this.id=`${this.type}.${this.subType}_${e}_${t}`}};const Wt=[W.Edit,W.Copy,W.Export,W.Comment],Gt=/* @__PURE__ */ new Set([G.Base,G.BaseTable,G.BaseField,G.BaseRecord,G.BaseView,G.BaseDashboard]),Kt=`table/`,qt=`field/`,Jt=`record/`,Yt=`view/`,Xt=`dashboard/`;function J(e){return`${Kt}${encodeURIComponent(e)}`}function Y(e,t){return`${qt}${encodeURIComponent(e)}/${encodeURIComponent(t)}`}function X(e,t){return`${Jt}${encodeURIComponent(e)}/${encodeURIComponent(t)}`}function Zt(e,t){return`${Yt}${encodeURIComponent(e)}/${encodeURIComponent(t)}`}function Qt(e){return`${Xt}${encodeURIComponent(e)}`}function $t(e,t,n){if(t===e)switch(n){case W.Edit:return new Lt(e);case W.Copy:return new It(e);case W.Export:return new Rt(e);case W.Comment:return new Ft(e);default:throw Error(`Unsupported Base permission action: ${n}`)}if(n!==W.Edit)throw Error(`Base object permissions only support Edit: ${t}`);if(t.startsWith(Kt))return new Ht(e,t);if(t.startsWith(qt))return new Bt(e,t);if(t.startsWith(Jt))return new Vt(e,t);if(t.startsWith(Yt))return new Ut(e,t);if(t.startsWith(Xt))return new zt(e,t);throw Error(`Unsupported Base permission object: ${t}`)}function en(e,t,n,r){var i;return((i=e.getPermissionPoint($t(t,n,r).id))==null?void 0:i.value)??!0}function tn(e,t,n,r,i){let a=$t(t,n,r);e.getPermissionPoint(a.id)||e.addPermissionPoint(a),e.updatePermissionPoint(a.id,i)}function nn(e,t,n,r){e.deletePermissionPoint($t(t,n,r).id)}function rn(e,t){e.getAllPermissionPoint().forEach((n,r)=>{n.subscribe(n=>{Gt.has(n.type)&&`unitId`in n&&n.unitId===t&&e.deletePermissionPoint(r)}).unsubscribe()})}function an(e,t,n){return en(e,t,t,W.Edit)&&Array.from(n).every(n=>en(e,t,n,W.Edit))}const on=tt(`base.permission.service`);let sn=class extends Re{constructor(e,t){super(),this._permissionService=e,this._instanceService=t,O(this,`_basePermissions`,/* @__PURE__ */ new Map),O(this,`_tablePermissions`,/* @__PURE__ */ new Map),O(this,`_fieldPermissions`,/* @__PURE__ */ new Map),O(this,`_recordPermissions`,/* @__PURE__ */ new Map),O(this,`_viewPermissions`,/* @__PURE__ */ new Map),O(this,`_tableBaseIds`,/* @__PURE__ */ new Map),this.disposeWithMe(this._instanceService.getTypeOfUnitDisposed$(H.UNIVER_BASE).subscribe(e=>this._clearUnit(e.getUnitId())))}canCreateTable(e){var t;return this._canEdit(e,[])&&(((t=this._basePermissions.get(e))==null?void 0:t.createTable)??!0)}canEditTable(e){return this._canEditTable(e)}canDeleteTable(e){var t;return this._canEditTable(e)&&(((t=this._tablePermissions.get(e))==null?void 0:t.delete)??!0)}canCreateField(e){var t;return this._canEditTable(e)&&(((t=this._tablePermissions.get(e))==null?void 0:t.createField)??!0)}canEditField(e,t){var n;return this._canEditTable(e)&&this._canEdit(e,[Y(e,t)])&&(((n=this._fieldPermissions.get(ln(e,t)))==null?void 0:n.edit)??!0)}canDeleteField(e,t){var n;return this.canEditField(e,t)&&(((n=this._fieldPermissions.get(ln(e,t)))==null?void 0:n.delete)??!0)}canCreateRecord(e){var t;return this._canEditTable(e)&&(((t=this._tablePermissions.get(e))==null?void 0:t.createRecord)??!0)}canEditRecord(e,t){var n;let r=this._recordPermissions.get(un(e,t));return this._canEdit(e,[J(e),X(e,t)])&&(((n=this._tablePermissions.get(e))==null?void 0:n.edit)??!0)&&((r==null?void 0:r.edit)??!0)&&((r==null?void 0:r.editCell)??!0)}canDeleteRecord(e,t){var n,r,i;return this._canEdit(e,[J(e),X(e,t)])&&(((n=this._tablePermissions.get(e))==null?void 0:n.edit)??!0)&&(((r=this._recordPermissions.get(un(e,t)))==null?void 0:r.edit)??!0)&&(((i=this._recordPermissions.get(un(e,t)))==null?void 0:i.delete)??!0)}canEditCell(e,t,n){var r,i,a;let o=this._fieldPermissions.get(ln(e,n));return this._canEdit(e,[J(e),Y(e,n),X(e,t)])&&(((r=this._tablePermissions.get(e))==null?void 0:r.edit)??!0)&&(((i=this._recordPermissions.get(un(e,t)))==null?void 0:i.edit)??!0)&&(((a=this._recordPermissions.get(un(e,t)))==null?void 0:a.editCell)??!0)&&((o==null?void 0:o.edit)??!0)&&((o==null?void 0:o.editCell)??!0)}canManageView(e,t){var n,r,i;return this._canEdit(e,[J(e),Zt(e,t)])&&(((n=this._tablePermissions.get(e))==null?void 0:n.edit)??!0)&&(((r=this._viewPermissions.get(dn(e,t)))==null?void 0:r.edit)??!0)&&(((i=this._tablePermissions.get(e))==null?void 0:i.manageView)??!0)}setBasePermission(e,t){this._basePermissions.set(e,{...this._basePermissions.get(e),...t}),t.edit!==void 0&&tn(this._permissionService,e,e,W.Edit,t.edit)}setTablePermission(e,t){this._rememberTable(e),this._tablePermissions.set(e,{...this._tablePermissions.get(e),...t}),this._setObjectEdit(e,J(e),t.edit)}setFieldPermission(e,t,n){this._rememberTable(e);let r=ln(e,t);this._fieldPermissions.set(r,{...this._fieldPermissions.get(r),...n}),this._setObjectEdit(e,Y(e,t),n.edit)}setRecordPermission(e,t,n){this._rememberTable(e);let r=un(e,t);this._recordPermissions.set(r,{...this._recordPermissions.get(r),...n}),this._setObjectEdit(e,X(e,t),n.edit)}setViewPermission(e,t,n){this._rememberTable(e);let r=dn(e,t);this._viewPermissions.set(r,{...this._viewPermissions.get(r),...n}),this._setObjectEdit(e,Zt(e,t),n.edit)}clearBasePermission(e){this._basePermissions.delete(e),nn(this._permissionService,e,e,W.Edit)}clearTablePermission(e){this._tablePermissions.delete(e),this._clearObjectEdit(e,J(e))}clearFieldPermission(e,t){this._fieldPermissions.delete(ln(e,t)),this._clearObjectEdit(e,Y(e,t))}clearRecordPermission(e,t){this._recordPermissions.delete(un(e,t)),this._clearObjectEdit(e,X(e,t))}clearViewPermission(e,t){this._viewPermissions.delete(dn(e,t)),this._clearObjectEdit(e,Zt(e,t))}getSnapshot(){return{bases:Object.fromEntries(this._basePermissions),tables:Object.fromEntries(this._tablePermissions),fields:Object.fromEntries(this._fieldPermissions),records:Object.fromEntries(this._recordPermissions),views:Object.fromEntries(this._viewPermissions)}}_canEditTable(e){var t;return this._canEdit(e,[J(e)])&&(((t=this._tablePermissions.get(e))==null?void 0:t.edit)??!0)}_canEdit(e,t){var n;let r=this._resolveBaseId(e);return!r||an(this._permissionService,r,t)&&(((n=this._basePermissions.get(r))==null?void 0:n.edit)??!0)}_setObjectEdit(e,t,n){if(n===void 0)return;let r=this._resolveBaseId(e);r&&tn(this._permissionService,r,t,W.Edit,n)}_clearObjectEdit(e,t){let n=this._resolveBaseId(e);n&&nn(this._permissionService,n,t,W.Edit)}_resolveBaseId(e){var t;let n=this._instanceService.getUnit(e,H.UNIVER_BASE);return n?n.getUnitId():(t=this._instanceService.getAllUnitsForType(H.UNIVER_BASE).find(t=>!!t.getSnapshot().tables[e]))==null?void 0:t.getUnitId()}_rememberTable(e){let t=this._resolveBaseId(e);t&&this._tableBaseIds.set(e,t)}_clearUnit(e){this._basePermissions.delete(e),this._tableBaseIds.forEach((t,n)=>{t===e&&(this._tablePermissions.delete(n),cn(this._fieldPermissions,`${n}\u001F`),cn(this._recordPermissions,`${n}\u001F`),cn(this._viewPermissions,`${n}\u001F`),this._tableBaseIds.delete(n))})}};sn=D([A(0,Ve),A(1,R)],sn);function cn(e,t){e.forEach((n,r)=>{r.startsWith(t)&&e.delete(r)})}function ln(e,t){return`${e}\u001F${t}`}function un(e,t){return`${e}\u001F${t}`}function dn(e,t){return`${e}\u001F${t}`}const Z=tt(`base.command.service`);let fn=class{constructor(e,t=pn(),n={}){this._permissionService=e,this._fieldRegistry=t,this._runtimeContext=n,O(this,`_factory`,new q),O(this,`_applier`,new i)}createTable(e,t,n){if(!this._permissionService.canCreateTable(e.getUnitId()))throw Error(`[BaseCommandService]: cannot create table.`);Qe(t),this._apply(e,this._factory.createTable(e.getSnapshot(),t,n))}renameTable(e,t,n){if(!this._permissionService.canEditTable(t))throw Error(`[BaseCommandService]: cannot edit table.`);this._apply(e,this._factory.renameTable(e.getSnapshot(),t,n))}deleteTable(e,t){if(!this._permissionService.canDeleteTable(t))throw Error(`[BaseCommandService]: cannot delete table.`);let n=pe(e.getSnapshot(),t);if(n.length>0)throw Error(`[BaseCommandService]: cannot delete a RecordLink target table (${n.map(e=>`${e.tableId}.${e.fieldId}`).join(`, `)}).`);this._apply(e,this._factory.deleteTable(e.getSnapshot(),t))}updateCell(e,t,n,r,i){this._apply(e,this.createUpdateCellOp(e,t,n,r,i))}setRecordValues(e,t,n){this._apply(e,this.createSetRecordValuesOp(e,t,n))}createRecord(e,t,n){this._apply(e,this.createCreateRecordOp(e,t,n))}createUpdateCellOp(e,t,n,r,i){let a=e.getSnapshot(),o=this._normalizeCellValue(a,t,n,r,i),s=this._withModifiedSystemValues(a,t,n,{[r]:o});return this._factory.setRecordValues(a,t,[s])}createSetRecordValuesOp(e,t,n){let r=e.getSnapshot(),i=n.map(e=>this._withModifiedSystemValues(r,t,e.recordId,{...Object.fromEntries(Object.entries(e.values).map(([n,i])=>[n,this._normalizeCellValue(r,t,e.recordId,n,i)]))}));return this._factory.setRecordValues(r,t,i)}createCreateRecordOp(e,t,n){if(!this._permissionService.canCreateRecord(t))throw Error(`[BaseCommandService]: cannot create record.`);let r=e.getSnapshot(),i=this._createNumberingAllocators(r.tables[t]);return this._factory.createRecord(r,t,this._createRecordWithDefaults(r,t,n,i))}createBatchCreateRecordsOp(e,t,n){if(!this._permissionService.canCreateRecord(t))throw Error(`[BaseCommandService]: cannot create record.`);let r=e.getSnapshot();return v(`BaseCommand.createBatchCreateRecordsOp`,()=>{let e=this._createNumberingAllocators(r.tables[t]);return this._factory.createRecords(r,t,n.map(n=>this._createRecordWithDefaults(r,t,n,e)))},{existingRecords:Object.keys(r.tables[t].records).length,records:n.length})}createCreateFieldOp(e,t,n,r,i){if(!this._permissionService.canCreateField(t))throw Error(`[BaseCommandService]: cannot create field.`);let a=e.getSnapshot();if(this._assertFieldDefinition(a,n),r===0)throw Error(`[BaseCommandService]: cannot insert a field before record-id.`);let o=this._factory.createField(a,t,{field:n,index:r,viewIndexes:i}),s=this._createSystemFieldValuePatches(a,t,n);if(!s.length)return o;let c=this._applier.applyOp(a,o);return h([o,this._factory.setRecordValues(c,t,s)])}deleteRecord(e,t,n){if(!this._permissionService.canDeleteRecord(t,n))throw Error(`[BaseCommandService]: cannot delete record.`);this._apply(e,this._factory.deleteRecord(e.getSnapshot(),t,n))}duplicateRecord(e,t,n,r){if(!this._permissionService.canCreateRecord(t))throw Error(`[BaseCommandService]: cannot create record.`);this._apply(e,this._factory.duplicateRecord(e.getSnapshot(),t,n,{...r,values:{...r.values,[M]:r.id}}))}updateRecordOrder(e,t,n,r){if(!this._permissionService.canEditRecord(t,n))throw Error(`[BaseCommandService]: cannot edit record.`);this._apply(e,this._factory.updateRecordOrder(e.getSnapshot(),t,n,r))}createField(e,t,n,r,i){this._apply(e,this.createCreateFieldOp(e,t,n,r,i))}updateField(e,t,n,r){var i;if(!this._permissionService.canEditField(t,n))throw Error(`[BaseCommandService]: cannot edit field.`);let a=e.getSnapshot(),o=(i=a.tables[t])==null?void 0:i.fields[n];if(!o||n===M)throw Error(`[BaseCommandService]: cannot edit record-id.`);this._assertFieldDefinition(a,{...o,...r}),this._apply(e,this._factory.updateField(a,t,n,r))}changeFieldType(e,t,n,r){var i;if(!this._permissionService.canEditField(t,n))throw Error(`[BaseCommandService]: cannot edit field.`);let a=e.getSnapshot(),o=(i=a.tables[t])==null?void 0:i.fields[n];if(!o||n===M)throw Error(`[BaseCommandService]: cannot change record-id field type.`);this._assertFieldDefinition(a,{...o,...r}),this._apply(e,this._factory.changeFieldType(a,t,n,r))}deleteField(e,t,n){var r;if(((r=e.getSnapshot().tables[t])==null?void 0:r.primaryFieldId)===n||n===M)throw Error(`[BaseCommandService]: cannot delete primary field.`);if(!this._permissionService.canDeleteField(t,n))throw Error(`[BaseCommandService]: cannot delete field.`);this._apply(e,this._factory.deleteField(e.getSnapshot(),t,n))}moveField(e,t,n,r){if(!this._permissionService.canEditField(t,n))throw Error(`[BaseCommandService]: cannot edit field.`);if(n===M||r.beforeFieldId===M)throw Error(`[BaseCommandService]: cannot move a field before record-id.`);this._apply(e,this._factory.moveField(e.getSnapshot(),t,n,r))}createView(e,t,n,r){if(!this._permissionService.canManageView(t,n.id))throw Error(`[BaseCommandService]: cannot manage view.`);this._apply(e,this._factory.createView(e.getSnapshot(),t,n,r))}renameView(e,t,n,r){if(!this._permissionService.canManageView(t,n))throw Error(`[BaseCommandService]: cannot manage view.`);this._apply(e,this._factory.renameView(e.getSnapshot(),t,n,r))}deleteView(e,t,n){if(!this._permissionService.canManageView(t,n))throw Error(`[BaseCommandService]: cannot manage view.`);this._apply(e,this._factory.deleteView(e.getSnapshot(),t,n))}moveView(e,t,n,r){if(!this._permissionService.canManageView(t,n))throw Error(`[BaseCommandService]: cannot manage view.`);this._apply(e,this._factory.moveView(e.getSnapshot(),t,n,r))}updateViewConfig(e,t,n,r){if(!this._permissionService.canManageView(t,n))throw Error(`[BaseCommandService]: cannot manage view.`);this._apply(e,this._factory.updateViewConfig(e.getSnapshot(),t,n,r))}setViewFilter(e,t,n,r){if(!this._permissionService.canManageView(t,n))throw Error(`[BaseCommandService]: cannot manage view.`);this._apply(e,this._factory.setViewFilter(e.getSnapshot(),t,n,r))}setViewSort(e,t,n,r){if(!this._permissionService.canManageView(t,n))throw Error(`[BaseCommandService]: cannot manage view.`);this._apply(e,this._factory.setViewSort(e.getSnapshot(),t,n,r))}setViewGroup(e,t,n,r){if(!this._permissionService.canManageView(t,n))throw Error(`[BaseCommandService]: cannot manage view.`);this._apply(e,this._factory.setViewGroup(e.getSnapshot(),t,n,r))}setViewFieldVisible(e,t,n,r,i){if(!this._permissionService.canManageView(t,n))throw Error(`[BaseCommandService]: cannot manage view.`);this._apply(e,this._factory.setViewFieldVisible(e.getSnapshot(),t,n,r,i))}setViewFieldWidth(e,t,n,r,i){if(!this._permissionService.canManageView(t,n))throw Error(`[BaseCommandService]: cannot manage view.`);this._apply(e,this._factory.setViewFieldWidth(e.getSnapshot(),t,n,r,i))}setViewFieldOrder(e,t,n,r,i){if(!this._permissionService.canManageView(t,n))throw Error(`[BaseCommandService]: cannot manage view.`);this._apply(e,this._factory.setViewFieldOrder(e.getSnapshot(),t,n,r,i))}_apply(e,t){let n=e.getSnapshot();e.setSnapshot(this._applier.applyOp(n,t))}_createRecordWithDefaults(e,t,n,r=this._createNumberingAllocators(e.tables[t])){let i=e.tables[t],a=n.createdBy??n.updatedBy??this._getCurrentUserId(),o={...n,createdBy:n.createdBy??a,updatedBy:n.updatedBy??a,values:{...n.values}};return o.values[M]=o.id,i.fieldOrder.forEach(e=>{let t=i.fields[e];if(t&&o.values[e]===void 0)switch(t.type){case N.Numbering:var n;o.values[e]=((n=r.get(e))==null?void 0:n())??this._createNumberingAllocator(i,e)();break;case N.CreatedBy:o.createdBy&&(o.values[e]=o.createdBy);break;case N.UpdatedBy:o.updatedBy&&(o.values[e]=o.updatedBy);break;case N.CreatedAt:o.values[e]=y(o.createdAt);break;case N.UpdatedAt:o.values[e]=y(o.updatedAt);break;default:{let n=t.defaultValue;n!=null&&n!==``&&!(Array.isArray(n)&&n.length===0)&&(o.values[e]=n)}}}),o}_withModifiedSystemValues(e,t,n,r){var i,a;let o=e.tables[t],s=((i=(a=this._runtimeContext).now)==null?void 0:i.call(a))??Date.now(),c=this._getCurrentUserId(),l={...r};return o.fieldOrder.forEach(e=>{let t=o.fields[e];t&&(t.type===N.UpdatedAt&&(l[e]=y(s)),t.type===N.UpdatedBy&&c&&(l[e]=c))}),{recordId:n,values:l,recordPatch:{updatedAt:s,...c?{updatedBy:c}:{}}}}_createSystemFieldValuePatches(e,t,n){let r=e.tables[t];return!r||!mn(n)?[]:n.type===N.Numbering?this._createNumberingFieldValuePatches(r,n.id):Object.values(r.records).flatMap(e=>{let t=this._getSystemFieldValue(n,e);return t===void 0?[]:[{recordId:e.id,values:{[n.id]:t}}]})}_getSystemFieldValue(e,t){switch(e.type){case N.CreatedBy:return t.createdBy??this._getCurrentUserId();case N.UpdatedBy:return t.updatedBy??this._getCurrentUserId();case N.CreatedAt:return y(t.createdAt??Date.now());case N.UpdatedAt:return y(t.updatedAt??Date.now());default:return}}_createNumberingAllocators(e){let t=/* @__PURE__ */ new Map;return e&&e.fieldOrder.forEach(n=>{let r=e.fields[n];(r==null?void 0:r.type)===N.Numbering&&t.set(n,this._createNumberingAllocator(e,n))}),t}_createNumberingAllocator(e,t){let n=Object.values(e.records).reduce((e,n)=>{let r=Number(n.values[t]);return Number.isFinite(r)?Math.max(e,r):e},0);return()=>String(++n)}_createNumberingFieldValuePatches(e,t){let n=this._getOrderedRecords(e),r=this._createNumberingAllocator(e,t);return n.flatMap(e=>this._isNonEmptyCellValue(e.values[t])?[]:[{recordId:e.id,values:{[t]:r()}}])}_getOrderedRecords(e){var t;let n=Object.values(e.records),r=new Map(n.map(e=>[e.id,e])),i=[],a=/* @__PURE__ */ new Set;(t=e.recordOrder)==null||t.forEach(e=>{let t=r.get(e);t&&(i.push(t),a.add(e))});let o=n.filter(e=>!a.has(e.id)).sort((e,t)=>String(e.orderKey??e.id).localeCompare(String(t.orderKey??t.id))||e.id.localeCompare(t.id));return[...i,...o]}_isNonEmptyCellValue(e){return e!=null&&e!==``&&!(Array.isArray(e)&&e.length===0)}_getCurrentUserId(){var e,t,n,r;return((e=(t=this._runtimeContext).getCurrentUserId)==null?void 0:e.call(t))??((n=(r=this._runtimeContext).getCurrentUser)==null||(n=n.call(r))==null?void 0:n.userID)??void 0}_normalizeCellValue(e,t,n,r,i){var a;if(!this._permissionService.canEditCell(t,n,r))throw Error(`[BaseCommandService]: cannot edit cell.`);let o=(a=e.tables[t])==null?void 0:a.fields[r];if(!o)throw Error(`[BaseCommandService]: field "${r}" does not exist.`);let s=this._fieldRegistry.get(o.type);if(o.readonly||o.system||!s.getCapabilities(o).editable)throw Error(`[BaseCommandService]: cannot edit readonly field.`);let c=s.normalize(i,o),l=s.validate(c,o);if(!l.valid)throw Error(l.reason??`[BaseCommandService]: invalid cell value.`);let u=o.type===N.RecordLink?E(e,o,c):{valid:!0};if(!u.valid)throw Error(u.reason??`[BaseCommandService]: invalid RecordLink value.`);return c}_assertFieldDefinition(e,t){if(t.id===M||t.type===N.RecordId||it(t.name))throw Error(`[BaseCommandService]: record-id is reserved.`);let n=ye(e,t);if(!n.valid)throw Error(n.reason??`[BaseCommandService]: invalid field definition.`)}};fn=D([A(0,on),A(1,ce)],fn);function pn(){let e=new fe;return te().forEach(t=>e.register(t)),e}function mn(e){return e.type===N.Numbering||e.type===N.CreatedBy||e.type===N.UpdatedBy||e.type===N.CreatedAt||e.type===N.UpdatedAt}const hn=U.createRegExpFromSafeFragment(String.raw`(?:^|\W)\$?[A-Z]{1,3}\$?\d+(?::\$?[A-Z]{1,3}\$?\d+)?(?:$|\W)`),gn=U.createRegExpFromSafeFragment(String.raw`(?:'\[[^\]]+\](?:[^']|'')+'|\[[^\]]+\][^\s'!]+)!\$?[A-Z]{1,3}\$?\d+(?::\$?[A-Z]{1,3}\$?\d+)?`,`gi`),_n=/\{([^}]+)\}/g,vn=U.createRegExpFromSafeFragment(String.raw`\b([A-Z_][\w.]*)\[\[\s*#(This Row|Data)\s*\],\s*\[([^\]]+)\]\]`,`gi`),yn=U.createRegExpFromSafeFragment(String.raw`\b([A-Z_][\w.]*)\[@\[([^\]]+)\]\]`,`gi`),bn=U.createRegExpFromSafeFragment(String.raw`\b([A-Z_][\w.]*)\[([^\[\]#]+)\]`,`gi`),xn=U.createRegExpFromSafeFragment(String.raw`(^|[^\w\[])\[\[\s*#(This Row|Data)\s*\],\s*\[([^\]]+)\]\]`,`gi`),Sn=U.createRegExpFromSafeFragment(String.raw`(^|[^\w\[])\[@\[([^\]]+)\]\](?!\])`,`g`),Cn=U.createRegExpFromSafeFragment(String.raw`(^|[^\w\[])\[@?([^\[\]#]+)\](?!\])`,`g`);var wn=class{createSuperTableReference(e,t=e.id){var n;let r=((n=e.recordOrder)==null?void 0:n.length)??Object.values(e.records).length;return{sheetId:t,titleMap:new Map(e.fieldOrder.map((t,n)=>{var r;return[((r=e.fields[t])==null?void 0:r.name)??t,n]})),range:{startRow:0,endRow:Math.max(0,r-1),startColumn:0,endColumn:Math.max(0,e.fieldOrder.length-1)},showHeader:!1}}getSuperTableRegistrations(e,t){let n=[],r=t?new Set(t):null,i=$e(e);return Object.values(e.tables).filter(e=>!r||r.has(e.id)).forEach(e=>{let t=this.createSuperTableReference(e,e.id);n.push({tableId:e.id,tableName:i.get(e.id),reference:t})}),n}validateFormula(e,t,n={}){let r=e.trim();if(!r)return{valid:!0};if(An(r))return{valid:!1,reason:`[BaseFormula]: A1 references and ranges are not supported in Base formulas.`};let i=n.snapshot?$e(n.snapshot):void 0;for(let e of kn(r)){let r=Rn(e,t,n.snapshot,i);if(!r.table)return{valid:!1,reason:`[BaseFormula]: table "${e.tableName}" does not exist. Use FBaseTable.getFormulaName() or getSchema().formulaName for structured references.`};if(!r.field)return{valid:!1,reason:`[BaseFormula]: field "${e.fieldName}" does not exist.`}}return{valid:!0}}getDependencies(e,t,n={}){let r=/* @__PURE__ */ new Set;for(let i of this.getStructuredDependencies(e,t,n))i.tableId===t.id&&r.add(i.fieldId);return Array.from(r)}getStructuredDependencies(e,t,n={}){let r=/* @__PURE__ */ new Map,i=n.snapshot?$e(n.snapshot):void 0;for(let a of kn(e)){let e=Rn(a,t,n.snapshot,i);if(!e.table||!e.field)continue;let o=`${e.table.id}\u001F${e.field.id}`;r.set(o,{tableId:e.table.id,fieldId:e.field.id})}return Array.from(r.values())}};function Tn(e,t){var n;let r=[],i=(n=t.unitData)==null?void 0:n[e.id];return i&&Object.entries(i).forEach(([t,n])=>{let i=e.tables[t];if(!i||!n)return;let a=k(i),o=[];new Ke(n).forValue((e,t,n)=>{let r=En(a,e,t,n);r&&o.push(r)}),o.length&&r.push({unitId:e.id,tableId:t,patches:o})}),r}function En(e,t,n,r){var i,a,o;let s=(i=e.rowId)==null?void 0:i[t],c=(a=e.colId)==null?void 0:a[n];if(!s||!e.records[s]||!c)return null;let l=e.fields[c];if(!l||l.type!==`formula`)return null;let u=String(((o=l.config)==null?void 0:o.formula)??``).trim(),d=(r==null?void 0:r.v)??null,f={v:d,t:On(d),...t===0&&u?{f:u}:{},si:l.id};return Dn(ue(e,s,c),f)?null:{row:t,col:n,value:f}}function Dn(e,t){return e!=null&&Object.is(e.v??null,t.v??null)&&e.t===t.t&&e.f===t.f&&e.si===t.si}function On(e){return typeof e==`number`?Pe.NUMBER:typeof e==`boolean`?Pe.BOOLEAN:typeof e==`string`?Pe.STRING:null}function kn(e){let t=Mn(e,()=>``),n=/* @__PURE__ */ new Map,r=e=>{n.set(`${e.tableName??``}\u001F${e.fieldName}`,e)};for(let e of t.matchAll(_n))r({fieldName:e[1],scope:`this-row`});t=t.replace(vn,(e,t,n,i)=>(r({tableName:t,fieldName:i,scope:n.toLowerCase()===`data`?`data`:`this-row`}),` `)),t=t.replace(yn,(e,t,n)=>(r({tableName:t,fieldName:n,scope:`this-row`}),` `)),t=t.replace(xn,(e,t,n,i)=>(r({fieldName:i,scope:n.toLowerCase()===`data`?`data`:`this-row`}),`${t} `)),t=t.replace(Sn,(e,t,n)=>(r({fieldName:n,scope:`this-row`}),`${t} `));for(let e of t.matchAll(bn))r({tableName:e[1],fieldName:e[2]});for(let e of t.matchAll(Cn))r({fieldName:e[2],scope:`this-row`});return Array.from(n.values())}function An(e){let t=Mn(e,()=>` `).replace(_n,` `).replace(vn,` `).replace(yn,` `).replace(xn,`$1 `).replace(Sn,`$1 `).replace(bn,` `).replace(Cn,`$1 `);return hn.test(t)}function jn(e,t){return In(Pn(e,t),t)}function Mn(e,t){return Nn(jn(e,t),t)}function Nn(e,t){return e.replace(/"(?:""|[^"])*"/g,t)}function Pn(e,t){return gn.lastIndex=0,e.replace(gn,(n,r)=>Ln(e,r)?n:t(n))}const Fn=/(?:'((?:[^']|'')+)'|\[[^\]]+\]|[A-Za-z0-9_.-]+)![^\s!\[\]]+\[/g;function In(e,t){let n=[];for(Fn.lastIndex=0;;){let t=Fn.exec(e);if(t==null)break;if(Ln(e,t.index))continue;let r=Fn.lastIndex-1,i=0;for(let a=r;a<e.length;a++)if(e[a]===`[`&&i++,e[a]===`]`&&(i--,i===0)){n.push({start:t.index,end:a+1}),Fn.lastIndex=a+1;break}}if(n.length===0)return e;let r=``,i=0;for(let a of n)r+=e.slice(i,a.start),r+=t(e.slice(a.start,a.end)),i=a.end;return r+e.slice(i)}function Ln(e,t){let n=!1;for(let r=0;r<t;r++)e[r]===`"`&&(n&&e[r+1]===`"`?r++:n=!n);return n}function Rn(e,t,n,r){let i=zn(e.tableName,t,n,r);return{table:i,field:i?Object.values(i.fields).find(t=>t.name===e.fieldName):void 0}}function zn(e,t,n,r){if(!e)return t;if(!n){var i;let n=e.toLowerCase();return n===t.id.toLowerCase()||n===t.name.toLowerCase()||n===((i=t.formulaName)==null?void 0:i.toLowerCase())?t:void 0}let a=e.toLowerCase(),o=r??$e(n),s=Object.values(n.tables).filter(e=>{var t;return e.id.toLowerCase()===a||e.name.toLowerCase()===a||((t=o.get(e.id))==null?void 0:t.toLowerCase())===a||Bn(e.id).toLowerCase()===a});return s.length===1?s[0]:void 0}function Bn(e){return`_T_${Array.from(e,e=>{var t;return/[A-Za-z0-9]/.test(e)?e:`_x${((t=e.codePointAt(0))==null?void 0:t.toString(16))??`0`}_`}).join(``)}`}const Vn=new wn;function Hn(e,t,n){var r;if(n.type!==N.Formula)return!0;let i=String(((r=n.config)==null?void 0:r.formula)??``),a=e.getSnapshot();return Vn.validateFormula(i,a.tables[t],{snapshot:a}).valid}function Q(e,t,n){let r=e.getSnapshot().tables[t];if(!r)return!1;let{viewIds:i,fieldIds:a,recordIds:o}=n||{};if(i){for(let e of i)if(!r.views[e])return!1}if(a){for(let e of a)if(!r.fields[e])return!1}if(o){for(let e of o)if(!r.records[e])return!1}return!0}function Un(e,t,n){if(n.id===M||n.type===N.RecordId||it(n.name)||!Hn(e,t,n))return!1;let r=e.getSnapshot();if(!ye(r,n).valid)return!1;if(n.type!==N.RecordLink)return!0;let i=ve(n);if(!i)return!1;if(i.relationRole===void 0)return!0;if(i.relationRole!==P.Parent||i.targetTableId!==t||i.multiple!==!1)return!1;let a=r.tables[t];return!Object.values(a.fields).some(e=>{var t;return e.id!==n.id&&e.type===N.RecordLink&&((t=ve(e))==null?void 0:t.relationRole)===P.Parent})}function Wn(e,t,n,r){var i;let a=e.getSnapshot(),o=(i=a.tables[t])==null?void 0:i.fields[n];if(!o||o.id===M||o.type===N.RecordId)return!1;if(o.type!==N.RecordLink)return!0;let s=qn(r);return E(a,o,s).valid}function Gn(e,t,n){let r=e.getSnapshot().tables[t];return!r||!at(n.id)||r.records[n.id]||Object.prototype.hasOwnProperty.call(n.values,M)&&n.values[M]!==n.id?!1:Object.entries(n.values).every(([n,r])=>n===M||Wn(e,t,n,r))}function Kn(e){return{...e,values:{...e.values,[M]:e.id}}}function qn(e){if(e&&typeof e==`object`&&!Array.isArray(e)&&`v`in e){let t=e.v;return t===void 0?null:t}return e}function Jn(e,t,n,r){let i=e.getSnapshot().tables[t];if(!i)return null;let a=u(i),o=a??_(i),c=n.some(e=>Object.prototype.hasOwnProperty.call(e.values,o)),l=a?i.fields[a]:c?s(i,o,r):null,d=[];for(let r of n){let n=r.values[o],i=l!==null&&Object.prototype.hasOwnProperty.call(r.values,o),a=qn(n),s=null;if(i){if(typeof a!=`string`||!he(a,!1).valid)return null;s=a}let c={};if(Object.entries(r.values).forEach(([e,t])=>{i&&e===o||(c[e]=t)}),!Gn(e,t,{...r,values:c}))return null;let u={...r.values};s!==null&&(u[o]=s),d.push(Kn({...r,values:u}))}return{records:d,hierarchyFieldToMaterialize:a?null:l,hierarchyChanges:se(i,d)}}const Yn={id:`base.command.batch-create-base-records`,type:F.COMMAND,handler(e,t){if(!t)return!1;let n=e.get(R),{unitId:r,tableId:a,records:o}=t,s=n.getUnit(r,H.UNIVER_BASE),c=o.map(e=>e.id);if(!s||!Q(s,a)||new Set(c).size!==c.length)return!1;let l=e.get(I),u=e.get(L),d=e.get(Z),f=s.getSnapshot(),p=Jn(s,a,o);if(!p)return!1;let{records:m,hierarchyChanges:g,hierarchyFieldToMaterialize:_}=p,v=_?d.createCreateFieldOp(s,a,_,f.tables[a].fieldOrder.length):null,y=d.createBatchCreateRecordsOp(s,a,m),b=h([...v?[v]:[],y]),x=new i().invertOp(f,b),S={unitId:r,op:b,hierarchyChanges:g.length?g:void 0,source:g.length?t.source??`ui`:void 0},ee={...S,source:g.length?`redo`:void 0},C={unitId:r,op:x,hierarchyChanges:g.length?g.map(e=>({...e,oldParentRecordId:e.parentRecordId,parentRecordId:null})):void 0,source:g.length?`undo`:void 0};return l.syncExecuteCommand(j.id,S)?(n.focusUnit(r),u.pushUndoRedo({unitID:r,undoMutations:[{id:j.id,params:C}],redoMutations:[{id:j.id,params:ee}]}),!0):!1}},Xn={id:`base.command.batch-delete-base-records`,type:F.COMMAND,handler:(e,t)=>{if(!t)return!1;let n=e.get(R),{unitId:r,tableId:a,recordIds:o}=t,s=n.getUnit(r,H.UNIVER_BASE);if(!s||!Q(s,a,{recordIds:o}))return!1;let c=e.get(I),l=e.get(L),u=s.getSnapshot(),d=ge(u.tables[a],o),f=new q,p=new i,m=f.deleteRecords(u,a,o),h=p.applyOp(u,m),g=f.createDeleteRecordsUndoOp(u,h,a,o),_={unitId:r,op:m,hierarchyChanges:d,source:t.source??`ui`},v={..._,source:`redo`},y={unitId:r,op:g,hierarchyChanges:d.map(e=>({...e,oldParentRecordId:e.parentRecordId,parentRecordId:e.oldParentRecordId})),source:`undo`};return c.syncExecuteCommand(j.id,_)?(n.focusUnit(r),l.pushUndoRedo({unitID:r,undoMutations:[{id:j.id,params:y}],redoMutations:[{id:j.id,params:v}]}),!0):!1}},Zn={id:`base.command.change-base-field-type`,type:F.COMMAND,handler:(e,t)=>{if(!t)return!1;let n=e.get(R),{unitId:r,tableId:a,fieldId:o,input:s}=t,c=n.getUnit(r,H.UNIVER_BASE);if(!c||o===M||!Q(c,a,{fieldIds:[o]}))return!1;let l=e.get(I),u=e.get(L),d=c.getSnapshot();if(!Un(c,a,{...d.tables[a].fields[o],...s}))return!1;let f=new q().changeFieldType(d,a,o,s),p=new i().invertOp(d,f),m={unitId:r,op:f},h={unitId:r,op:p};return l.syncExecuteCommand(j.id,m)?(n.focusUnit(r),u.pushUndoRedo({unitID:r,undoMutations:[{id:j.id,params:h}],redoMutations:[{id:j.id,params:m}]}),!0):!1}},Qn={id:`base.command.create-base-child-record`,type:F.COMMAND,handler(e,t){var n;if(!t)return!1;let{unitId:r,tableId:a,parentRecordId:o}=t,l=e.get(R),d=l.getUnit(r,H.UNIVER_BASE);if(!d||!Q(d,a,{recordIds:[o]}))throw new c(`missingRecord`);let f=d.getSnapshot(),p=f.tables[a],m=u(p),g=m??t.fieldId??_(p);if(m&&t.fieldId&&t.fieldId!==m||p.fields[g]&&!C(p,g))throw new c(`invalidField`);if((ee(p,g).depthByRecordId.get(o)??0)+1>=5)throw new c(`maxDepth`);if(!Gn(d,a,t.record))return!1;let v=Kn({...t.record,values:{...t.record.values,[g]:re([o])}}),y=e.get(Z),b=new q,x=p.fields[g],S=x?((n=ve(x))==null?void 0:n.relationRole)===P.Parent?null:b.updateField(f,a,g,{config:{...x.config,relationRole:P.Parent}}):y.createCreateFieldOp(d,a,s(p,g,t.parentFieldName),p.fieldOrder.length),w=y.createCreateRecordOp(d,a,v),T=h([...S?[S]:[],w]),te=new i().invertOp(f,T),ne={tableId:a,fieldId:g,recordId:v.id,oldParentRecordId:null,parentRecordId:o},ie={unitId:r,op:T,trigger:Qn.id,hierarchyChanges:[ne],source:t.source??`ui`},ae={...ie,source:`redo`},E={unitId:r,op:te,trigger:Qn.id,hierarchyChanges:[{...ne,oldParentRecordId:o,parentRecordId:null}],source:`undo`};return e.get(I).syncExecuteCommand(j.id,ie)?(l.focusUnit(r),e.get(L).pushUndoRedo({unitID:r,undoMutations:[{id:j.id,params:E}],redoMutations:[{id:j.id,params:ae}]}),!0):!1}},$n={id:`base.command.create-base-field`,type:F.COMMAND,handler(e,t){if(!t)return!1;let n=e.get(R),{unitId:r,tableId:a,field:o,index:s,viewIndexes:c}=t,l=n.getUnit(r,H.UNIVER_BASE);if(!l||!Q(l,a)||l.getSnapshot().tables[a].fields[o.id]||s===0||!Un(l,a,o))return!1;let u=e.get(I),d=e.get(L),f=e.get(Z).createCreateFieldOp(l,a,o,s,c),p=new i().invertOp(l.getSnapshot(),f),m={unitId:r,op:f},h={unitId:r,op:p};return u.syncExecuteCommand(j.id,m)?(n.focusUnit(r),d.pushUndoRedo({unitID:r,undoMutations:[{id:j.id,params:h}],redoMutations:[{id:j.id,params:m}]}),!0):!1}},er={id:`base.command.create-base-record`,type:F.COMMAND,handler(e,t){if(!t)return!1;let n=e.get(R),{unitId:r,tableId:a,record:o}=t,s=n.getUnit(r,H.UNIVER_BASE);if(!s||!Q(s,a))return!1;let c=e.get(I),l=e.get(L),u=e.get(Z),d=s.getSnapshot(),f=Jn(s,a,[o]);if(!f)return!1;let[p]=f.records,{hierarchyChanges:m,hierarchyFieldToMaterialize:g}=f,_=g?u.createCreateFieldOp(s,a,g,d.tables[a].fieldOrder.length):null,v=u.createCreateRecordOp(s,a,p),y=h([..._?[_]:[],v]),b=new i().invertOp(d,y),x={unitId:r,op:y,hierarchyChanges:m.length?m:void 0,source:m.length?t.source??`ui`:void 0},S={...x,source:m.length?`redo`:void 0},ee={unitId:r,op:b,hierarchyChanges:m.length?m.map(e=>({...e,oldParentRecordId:e.parentRecordId,parentRecordId:null})):void 0,source:m.length?`undo`:void 0};return c.syncExecuteCommand(j.id,x)?(n.focusUnit(r),l.pushUndoRedo({unitID:r,undoMutations:[{id:j.id,params:ee}],redoMutations:[{id:j.id,params:S}]}),!0):!1}},tr=`Base table names are exported as Excel worksheet names and must contain 1-31 characters, not start or end with an apostrophe, not contain : \\ / ? * [ ], and be unique (case-insensitive).`;function nr(e,t,n){if(!t.trim())return{valid:!1,reason:`Table name is required.`,error:`required`};if(t.length>31)return{valid:!1,reason:`Use 31 characters or fewer.`,error:`too-long`};if(t.startsWith(`'`)||t.endsWith(`'`))return{valid:!1,reason:`A table name cannot start or end with an apostrophe.`,error:`apostrophe`};if(/[:\\/?*\[\]]/.test(t))return{valid:!1,reason:`A table name cannot contain : \\ / ? * [ ].`,error:`invalid-characters`};if(!st(t))return{valid:!1,reason:`Choose another name.`,error:`invalid`};let r=t.toLowerCase();return Object.values(e.tables).some(e=>e.id!==n&&e.name.toLowerCase()===r)?{valid:!1,reason:`A table with this name already exists.`,error:`duplicate`}:{valid:!0}}function rr(e,t){let n=t.replace(/[:\\/?*\[\]]/g,` `).replace(/^'+|'+$/g,``).trim()||`Table`,r=1;for(;;){let t=r===1?``:` ${r}`,i=`${n.slice(0,31-t.length)}${t}`;if(nr(e,i).valid)return i;r++}}const ir={id:`base.command.create-base-table`,type:F.COMMAND,handler:(e,t)=>{if(!t)return!1;let n=e.get(R),{unitId:r,table:a,index:o}=t,s=n.getUnit(r,H.UNIVER_BASE);if(!s||s.getSnapshot().tables[a.id]||!nr(s.getSnapshot(),a.name).valid)return!1;try{Qe(a)}catch{return!1}let c=e.get(I),l=e.get(L),u=s.getSnapshot(),d=new q().createTable(u,a,o),f=new i().invertOp(u,d),p={unitId:r,op:d},m={unitId:r,op:f};return c.syncExecuteCommand(j.id,p)?(n.focusUnit(r),l.pushUndoRedo({unitID:r,undoMutations:[{id:j.id,params:m}],redoMutations:[{id:j.id,params:p}]}),!0):!1}};function ar(e,t,n=[]){let r=/* @__PURE__ */ new Set([...Object.values(e.fields).filter(e=>e).map(e=>e.name),...n.map(e=>e.name)]);if(!r.has(t))return t;let i=2;for(;r.has(`${t} ${i}`);)i++;return`${t} ${i}`}function or(e,t){if(!e||!t.length)return;let n=[...e];return t.forEach(e=>{n.includes(e)||n.push(e)}),n}const sr={options:[{id:`todo`,name:`Todo`,color:`#BFDBFE`},{id:`doing`,name:`Doing`,color:`#FDE68A`},{id:`done`,name:`Done`,color:`#BBF7D0`}]},cr={locale:`en-US`,pattern:`yyyy/mm/dd`,includeTime:!1,hourCycle:`h24`,defaultMode:`none`};function lr(e,t,n,r){var i;let a=new q,o=e.tables[t],s=o?{...n,fieldOrder:[M,...(n.fieldOrder??o.fieldOrder).filter(e=>e!==M)],fieldSettings:{...n.fieldSettings,[M]:{...(i=n.fieldSettings)==null?void 0:i[M],hidden:!0}}}:n;if(o&&s.type===`kanban`){let n=dr(o,s),i=n.changed?{...s,config:n.config,fieldOrder:n.fieldOrder??s.fieldOrder}:s;return h([...n.fields.map(n=>a.createField(e,t,{field:n})),a.createView(e,t,i,r)])}if(!o||s.type!==`calendar`&&s.type!==`gantt`)return a.createView(e,t,s,r);let c=ur(o,s),l=c.changed?{...s,config:c.config,fieldOrder:c.fieldOrder??s.fieldOrder}:s;return s.type===`gantt`&&mr(s)&&(l=hr(o,l,c.fields)),h([...c.fields.map(n=>a.createField(e,t,{field:n})),a.createView(e,t,l,r)])}function ur(e,t,n=!1){var r,i;let a=e.fieldOrder.filter(t=>_r(e,t)),o=[],s={...t.config??{}},c=typeof s.startDateFieldId==`string`&&_r(e,s.startDateFieldId)?s.startDateFieldId:a[0],l=typeof s.endDateFieldId==`string`&&_r(e,s.endDateFieldId)&&s.endDateFieldId!==c?s.endDateFieldId:a.find(e=>e!==c);if(!c){let n=vr(e,o,t.type===`gantt`?`Start date`:`Date`);o.push(n),c=n.id}if((t.type===`gantt`||n)&&!l){let t=vr(e,o,`End date`);o.push(t),l=t.id}t.type===`calendar`&&!l&&delete s.endDateFieldId,s.startDateFieldId=c,t.type===`calendar`&&l&&(s.endDateFieldId=l),t.type===`gantt`&&(s.endDateFieldId=l??c);let u=or(t.fieldOrder,o.map(e=>e.id));return{changed:o.length>0||s.startDateFieldId!==((r=t.config)==null?void 0:r.startDateFieldId)||s.endDateFieldId!==((i=t.config)==null?void 0:i.endDateFieldId)||!(!u||V.diffValue(u,t.fieldOrder??[])),fields:o,config:s,fieldOrder:u}}function dr(e,t){var n;let r=[],i={...t.config??{}},a=typeof i.groupFieldId==`string`&&fr(e,i.groupFieldId)?i.groupFieldId:e.fieldOrder.find(t=>fr(e,t));if(!a){let t=pr(e,r);r.push(t),a=t.id}i.groupFieldId=a;let o=or(t.fieldOrder,r.map(e=>e.id));return{changed:r.length>0||i.groupFieldId!==((n=t.config)==null?void 0:n.groupFieldId)||!(!o||V.diffValue(o,t.fieldOrder??[])),fields:r,config:i,fieldOrder:o}}function fr(e,t){let n=t?e.fields[t]:null;return n?[N.SingleSelect,N.MultiSelect,N.Rating,N.Person,N.Group,N.Checkbox].includes(n.type):!1}function pr(e,t){return{id:`fld-${nt(8)}`,name:ar(e,`Status`,t),type:N.SingleSelect,config:V.deepClone(sr)}}function mr(e){return Object.keys(e.config??{}).length===0&&Object.keys(e.fieldSettings??{}).length===0}function hr(e,t,n){let r={...t.config??{}},i=n.map(e=>e.id),a=[...e.fieldOrder,...i],o=typeof r.titleFieldId==`string`&&gr(e,n,r.titleFieldId)?r.titleFieldId:e.primaryFieldId,s=typeof r.startDateFieldId==`string`?r.startDateFieldId:``,c=typeof r.endDateFieldId==`string`?r.endDateFieldId:s,l=new Set([o,s,c].filter(t=>t&&gr(e,n,t))),u=Object.fromEntries(a.flatMap(t=>l.has(t)||!gr(e,n,t)?[]:[[t,{hidden:!0}]]));return{...t,config:{...r,titleFieldId:o},fieldSettings:u}}function gr(e,t,n){return!!(e.fields[n]??t.find(e=>e.id===n))&&!0}function _r(e,t){let n=t?e.fields[t]:null;return!!n&&n.type===N.Date&&n.readonly!==!0&&n.system!==!0}function vr(e,t,n){let r=/* @__PURE__ */ new Set([...Object.keys(e.fields),...t.map(e=>e.id)]),i=`fld-${nt(8)}`;for(;r.has(i);)i=`fld-${nt(8)}`;return{id:i,name:ar(e,n,t),type:N.Date,config:{...cr}}}const yr={id:`base.command.create-base-view`,type:F.COMMAND,handler:(e,t)=>{if(!t)return!1;let n=e.get(R),{unitId:r,tableId:a,view:o,index:s}=t,c=n.getUnit(r,H.UNIVER_BASE);if(!c||!Q(c,a)||c.getSnapshot().tables[a].views[o.id])return!1;let l=e.get(I),u=e.get(L),d=c.getSnapshot(),f=lr(d,a,o,s),p=new i().invertOp(d,f),m={historyAction:`create-view`,unitId:r,op:f,viewType:o.type},h={historyAction:`delete-view`,unitId:r,op:p,viewType:o.type};return l.syncExecuteCommand(j.id,m)?(n.focusUnit(r),u.pushUndoRedo({unitID:r,undoMutations:[{id:j.id,params:h}],redoMutations:[{id:j.id,params:m}]}),!0):!1}},br={id:`base.command.delete-base-field`,type:F.COMMAND,handler(e,t){if(!t)return!1;let n=e.get(R),{unitId:r,tableId:a,fieldId:o}=t,s=n.getUnit(r,H.UNIVER_BASE);if(!s)return!1;let c=s.getSnapshot(),l=c.tables[a];if(!Q(s,a,{fieldIds:[o]})||l.primaryFieldId===o||o===M)return!1;let u=e.get(I),d=e.get(L),f=new q().deleteField(c,a,o),p=new i().invertOp(c,f),m={unitId:r,op:f},h={unitId:r,op:p};return u.syncExecuteCommand(j.id,m)?(n.focusUnit(r),d.pushUndoRedo({unitID:r,undoMutations:[{id:j.id,params:h}],redoMutations:[{id:j.id,params:m}]}),!0):!1}},xr={id:`base.command.delete-base-record`,type:F.COMMAND,handler:(e,t)=>{if(!t)return!1;let n=e.get(R),{unitId:r,tableId:a,recordId:o}=t,s=n.getUnit(r,H.UNIVER_BASE);if(!s||!Q(s,a,{recordIds:[o]}))return!1;let c=e.get(I),l=e.get(L),u=s.getSnapshot(),d=ge(u.tables[a],[o]),f=new q,p=new i,m=f.deleteRecord(u,a,o),h=p.applyOp(u,m),g=f.createDeleteRecordsUndoOp(u,h,a,[o]),_={unitId:r,op:m,hierarchyChanges:d,source:t.source??`ui`},v={..._,source:`redo`},y={unitId:r,op:g,hierarchyChanges:d.map(e=>({...e,oldParentRecordId:e.parentRecordId,parentRecordId:e.oldParentRecordId})),source:`undo`};return c.syncExecuteCommand(j.id,_)?(n.focusUnit(r),l.pushUndoRedo({unitID:r,undoMutations:[{id:j.id,params:y}],redoMutations:[{id:j.id,params:v}]}),!0):!1}},Sr={id:`base.command.delete-base-table`,type:F.COMMAND,handler:(e,t)=>{if(!t)return!1;let n=e.get(R),{unitId:r,tableId:a}=t,o=n.getUnit(r,H.UNIVER_BASE);if(!o||!Q(o,a))return!1;let s=e.get(I),c=e.get(L),l=o.getSnapshot(),u=pe(l,a);if(u.length>0)return console.error(`[DeleteBaseTableCommand]: table "${a}" is referenced by ${u.map(e=>`${e.tableId}.${e.fieldId}`).join(`, `)}.`),!1;let d=new q().deleteTable(l,a),f=new i().invertOp(l,d),p={unitId:r,op:d},m={unitId:r,op:f};return s.syncExecuteCommand(j.id,p)?(n.focusUnit(r),c.pushUndoRedo({unitID:r,undoMutations:[{id:j.id,params:m}],redoMutations:[{id:j.id,params:p}]}),!0):!1}},Cr={id:`base.command.delete-base-view`,type:F.COMMAND,handler:(e,t)=>{if(!t)return!1;let n=e.get(R),{unitId:r,tableId:a,viewId:o}=t,s=n.getUnit(r,H.UNIVER_BASE);if(!s||!Q(s,a,{viewIds:[o]}))return!1;let c=e.get(I),l=e.get(L),u=s.getSnapshot(),d=u.tables[a].views[o].type,f=new q().deleteView(u,a,o),p=new i().invertOp(u,f),m={historyAction:`delete-view`,unitId:r,op:f,viewType:d},h={historyAction:`create-view`,unitId:r,op:p,viewType:d};return c.syncExecuteCommand(j.id,m)?(n.focusUnit(r),l.pushUndoRedo({unitID:r,undoMutations:[{id:j.id,params:h}],redoMutations:[{id:j.id,params:m}]}),!0):!1}},wr={id:`base.command.duplicate-base-field`,type:F.COMMAND,handler:(e,t)=>{if(!t)return!1;let n=e.get(R),{unitId:r,tableId:a,sourceFieldId:o,field:s,index:c,viewIndexes:l}=t,u=n.getUnit(r,H.UNIVER_BASE);if(!u||!Q(u,a,{fieldIds:[o]})||o===M||u.getSnapshot().tables[a].fields[s.id]||c===0||!Un(u,a,s))return!1;let d=e.get(I),f=e.get(L),p=u.getSnapshot(),m=new q().duplicateField(p,a,o,{field:s,index:c,viewIndexes:l}),h=new i().invertOp(p,m),g={unitId:r,op:m},_={unitId:r,op:h};return d.syncExecuteCommand(j.id,g)?(n.focusUnit(r),f.pushUndoRedo({unitID:r,undoMutations:[{id:j.id,params:_}],redoMutations:[{id:j.id,params:g}]}),!0):!1}},Tr={id:`base.command.duplicate-base-record`,type:F.COMMAND,handler:(e,t)=>{if(!t)return!1;let n=e.get(R),{unitId:r,tableId:a,sourceRecordId:o,record:s}=t,c=n.getUnit(r,H.UNIVER_BASE);if(!c||!Q(c,a,{recordIds:[o]})||!Gn(c,a,s))return!1;let l=e.get(I),u=e.get(L),d=c.getSnapshot(),f=Kn(s),p=se(d.tables[a],[f]),m=new q().duplicateRecord(d,a,o,f),h=new i().invertOp(d,m),g={unitId:r,op:m,hierarchyChanges:p.length?p:void 0,source:p.length?t.source??`ui`:void 0},_={...g,source:p.length?`redo`:void 0},v={unitId:r,op:h,hierarchyChanges:p.length?p.map(e=>({...e,oldParentRecordId:e.parentRecordId,parentRecordId:null})):void 0,source:p.length?`undo`:void 0};return l.syncExecuteCommand(j.id,g)?(n.focusUnit(r),u.pushUndoRedo({unitID:r,undoMutations:[{id:j.id,params:v}],redoMutations:[{id:j.id,params:_}]}),!0):!1}},Er={id:`base.command.ensure-base-temporal-view-date-fields`,type:F.COMMAND,handler(e,t){if(!t)return!1;let n=e.get(R),{unitId:r,tableId:a,viewId:o,ensureEndDateField:s}=t,c=n.getUnit(r,H.UNIVER_BASE);if(!c||!Q(c,a,{viewIds:[o]}))return!1;let l=c.getSnapshot(),u=l.tables[a],d=u==null?void 0:u.views[o];if(!u||!d||d.type!==`calendar`&&d.type!==`gantt`)return!1;let f=ur(u,d,s===!0);if(!f.changed)return!1;let p=e.get(I),m=e.get(L),g=new q,_=[...f.fields.map(e=>g.createField(l,t.tableId,{field:e})),Dr([...Or(t.tableId,t.viewId),`config`],d.config,f.config)];f.fieldOrder&&_.push(Dr([...Or(t.tableId,t.viewId),`fieldOrder`],d.fieldOrder,f.fieldOrder));let v=h(_),y=new i().invertOp(l,v),b={unitId:r,op:v},x={unitId:r,op:y};return p.syncExecuteCommand(j.id,b)?(n.focusUnit(r),m.pushUndoRedo({unitID:r,undoMutations:[{id:j.id,params:x}],redoMutations:[{id:j.id,params:b}]}),!0):!1}};function Dr(e,t,n){return t===void 0?z.insertOp(e,n):z.replaceOp(e,t,n)}function Or(e,t){return[`tables`,e,`views`,t]}const kr={id:`base.command.move-base-field`,type:F.COMMAND,handler:(e,t)=>{if(!t)return!1;let n=e.get(R),{unitId:r,tableId:a,fieldId:o,target:s}=t,c=n.getUnit(r,H.UNIVER_BASE),l=s.beforeFieldId??s.afterFieldId;if(!c||!!s.beforeFieldId==!!s.afterFieldId||!l||l===o||o===M||s.beforeFieldId===M||!Q(c,a,{fieldIds:[o,l]}))return!1;let u=e.get(I),d=e.get(L),f=c.getSnapshot(),p=new q().moveField(f,a,o,s),m=new i().invertOp(f,p),h={unitId:r,op:p},g={unitId:r,op:m};return u.syncExecuteCommand(j.id,h)?(n.focusUnit(r),d.pushUndoRedo({unitID:r,undoMutations:[{id:j.id,params:g}],redoMutations:[{id:j.id,params:h}]}),!0):!1}},Ar={id:`base.command.move-base-hierarchy-record`,type:F.COMMAND,handler:(t,n)=>{var r;if(!n)return!1;let{unitId:a,tableId:o,recordId:l,parentRecordId:d,orderKey:f}=n,p=t.get(R),m=p.getUnit(a,H.UNIVER_BASE);if(!m||!Q(m,o,{recordIds:[l,...d?[d]:[]]}))throw new c(`missingRecord`);let g=m.getSnapshot(),v=g.tables[o],y=u(v),b=y??n.fieldId??_(v);if(y&&n.fieldId&&n.fieldId!==y||v.fields[b]&&!C(v,b))throw new c(`invalidField`);let x=ee(v,b),S=e(x,l,d);if(!S.valid){let e=S.reason===Me.SelfParent?`selfParent`:S.reason===Me.Cycle?`cycle`:`maxDepth`;throw new c(e)}let w=x.parentByRecordId.get(l)??null,T=v.records[l].orderKey;if(w===d&&(f===void 0||f===T))return!0;let te=w!==d,ne=f!==void 0&&f!==T,ie=new q,ae=[],E=v.fields[b],oe=te&&(!E||((r=ve(E))==null?void 0:r.relationRole)!==P.Parent)?E?ie.updateField(g,o,b,{config:{...E.config,relationRole:P.Parent}}):t.get(Z).createCreateFieldOp(m,o,s(v,b,n.parentFieldName),v.fieldOrder.length):null;oe&&ae.push(oe),te&&ae.push(ie.updateCell(g,o,l,b,d?re([d]):``)),ne&&ae.push(ie.updateRecordOrder(g,o,l,f));let se=h(ae),D={tableId:o,fieldId:b,recordId:l,oldParentRecordId:w,parentRecordId:d},O={unitId:a,op:se,trigger:Ar.id,hierarchyChanges:[D],source:n.source??`ui`},ce={...O,source:`redo`};if(!t.get(I).syncExecuteCommand(j.id,O))return!1;let le=m.getSnapshot(),ue=[];te&&ue.push(jr(le,g,o,l,b)),ne&&ue.push(Mr(le,g,o,l)),oe&&ue.push(new i().invertOp(g,oe));let k={unitId:a,op:h(ue),trigger:Ar.id,hierarchyChanges:[{...D,oldParentRecordId:d,parentRecordId:w}],source:`undo`};return p.focusUnit(a),t.get(L).pushUndoRedo({unitID:a,undoMutations:[{id:j.id,params:k}],redoMutations:[{id:j.id,params:ce}]}),!0}};function jr(e,t,n,r,i){var a,o,s,c,l,u,d,f;let p=e.tables[n],m=t.tables[n],g=(a=p.rowIndex)==null?void 0:a[r],_=(o=p.colIndex)==null?void 0:o[i],v=(s=m.rowIndex)==null?void 0:s[r],y=(c=m.colIndex)==null?void 0:c[i],b=g==null||_==null||(l=p.cellData)==null||(l=l[g])==null?void 0:l[_],x=v==null||y==null||(u=m.cellData)==null||(u=u[v])==null?void 0:u[y],S=(d=p.records[r])==null?void 0:d.values[i],ee=(f=m.records[r])==null?void 0:f.values[i],C=[],w=Nr(g==null||_==null?null:K.cellData(n,g,_),b,x);w&&C.push(w);let T=Nr(K.cell(n,r,i),S,ee);return T&&C.push(T),h(C)}function Mr(e,t,n,r){var i,a;let o=e.tables[n],s=t.tables[n],c=[Nr([...K.record(n,r),`orderKey`],(i=o.records[r])==null?void 0:i.orderKey,(a=s.records[r])==null?void 0:a.orderKey),Nr([...K.table(n),`recordOrder`],o.recordOrder,s.recordOrder)].filter(e=>!!e);return h(c)}function Nr(e,t,n){if(!e||t===void 0&&n===void 0)return null;let r=Ge.getInstance();return n===void 0?r.removeOp(e,t):t===void 0?r.insertOp(e,n):r.replaceOp(e,t,n)}const Pr={id:`base.command.move-base-view`,type:F.COMMAND,handler:(e,t)=>{if(!t)return!1;let n=e.get(R),{unitId:r,tableId:a,viewId:o,target:s}=t,c=n.getUnit(r,H.UNIVER_BASE),l=s.beforeViewId??s.afterViewId;if(!c||!l||l===o||!Q(c,a,{viewIds:[o,l]}))return!1;let u=e.get(I),d=e.get(L),f=c.getSnapshot(),p=new q().moveView(f,a,o,s),m=new i().invertOp(f,p),h={unitId:r,op:p},g={unitId:r,op:m};return u.syncExecuteCommand(j.id,h)?(n.focusUnit(r),d.pushUndoRedo({unitID:r,undoMutations:[{id:j.id,params:g}],redoMutations:[{id:j.id,params:h}]}),!0):!1}},Fr={id:`base.command.rename-base-table`,type:F.COMMAND,handler:(e,t)=>{if(!t)return!1;let n=e.get(R),{unitId:r,tableId:a,name:o}=t,s=n.getUnit(r,H.UNIVER_BASE);if(!s||!Q(s,a)||!nr(s.getSnapshot(),o,a).valid)return!1;let c=e.get(I),l=e.get(L),u=s.getSnapshot(),d=new q().renameTable(u,a,o),f=new i().invertOp(u,d),p={unitId:r,op:d},m={unitId:r,op:f};return c.syncExecuteCommand(j.id,p)?(n.focusUnit(r),l.pushUndoRedo({unitID:r,undoMutations:[{id:j.id,params:m}],redoMutations:[{id:j.id,params:p}]}),!0):!1}},Ir={id:`base.command.rename-base-view`,type:F.COMMAND,handler:(e,t)=>{if(!t)return!1;let n=e.get(R),{unitId:r,tableId:a,viewId:o,name:s}=t,c=n.getUnit(r,H.UNIVER_BASE);if(!c||!Q(c,a,{viewIds:[o]}))return!1;let l=e.get(I),u=e.get(L),d=c.getSnapshot(),f=new q().renameView(d,a,o,s),p=new i().invertOp(d,f),m={unitId:r,op:f},h={unitId:r,op:p};return l.syncExecuteCommand(j.id,m)?(n.focusUnit(r),u.pushUndoRedo({unitID:r,undoMutations:[{id:j.id,params:h}],redoMutations:[{id:j.id,params:m}]}),!0):!1}},Lr={id:`base.command.set-base-kanban-group-field`,type:F.COMMAND,handler:(e,t)=>{if(!t)return!1;let n=e.get(R),{unitId:r,tableId:a,viewId:o,fieldId:s}=t,c=n.getUnit(r,H.UNIVER_BASE);if(!c||!Q(c,a,{viewIds:[o],fieldIds:[s]}))return!1;let l=e.get(I),u=e.get(L),d=c.getSnapshot(),f=new q().updateViewConfig(d,a,o,{groupFieldId:s}),p=new i().invertOp(d,f),m={unitId:r,op:f},h={unitId:r,op:p};return l.syncExecuteCommand(j.id,m)?(n.focusUnit(r),u.pushUndoRedo({unitID:r,undoMutations:[{id:j.id,params:h}],redoMutations:[{id:j.id,params:m}]}),!0):!1}},Rr={id:`base.command.set-base-name`,type:F.COMMAND,handler:(e,t)=>{if(!t)return!1;let n=e.get(R),r=n.getUnit(t.unitId,H.UNIVER_BASE);if(!r)return!1;let a=r.getSnapshot().name;if(a===t.name)return!0;let o=new q,s=new i,c=[],l=[],u=[r,...n.getAllUnitsForType(H.UNIVER_BASE).filter(e=>e.getUnitId()!==t.unitId)];for(let e of u){let n=e.getSnapshot(),i=e===r?o.setBaseName(n,t.name):o.renameFormulaUnitReferences(n,a,t.name);i&&(c.push({id:j.id,params:{unitId:e.getUnitId(),op:i,trigger:Rr.id}}),l.unshift({id:j.id,params:{unitId:e.getUnitId(),op:s.invertOp(n,i),trigger:Rr.id}}))}let d=e.get(I),f=e.get(L),p=f.__tempBatchingUndoRedo(t.unitId);try{return lt(c,d).result?(n.focusUnit(t.unitId),f.pushUndoRedo({unitID:t.unitId,undoMutations:l,redoMutations:c}),!0):!1}finally{p.dispose()}}},zr={type:F.COMMAND,id:`base.command.set-permission`,async handler(e,t){if(!t||!Wt.includes(t.action)||t.objectId!==t.unitId&&t.action!==W.Edit||t.policy&&(![`all`,`owner`,`members`].includes(t.policy.edit)||t.policy.strategies.some(e=>!Wt.includes(e.action)||t.objectId!==t.unitId&&e.action!==W.Edit)))return!1;let n=$t(t.unitId,t.objectId,t.action),r={unitId:t.unitId,objectId:t.objectId,objectType:n.type},i=e.get(Je);return t.remove?await i.remove(r):t.policy?await i.save(r,t.policy):await i.setPoint(r,n,t.value),!0}},Br={type:F.COMMAND,id:`base.command.set-permissions`,async handler(e,t){if(!(t!=null&&t.unitId)||!Array.isArray(t.changes)||!t.changes.length||t.changes.some(e=>!(e!=null&&e.objectId)||e.objectId===t.unitId||e.policy!==null&&(!e.policy||![`all`,`owner`,`members`].includes(e.policy.edit)||!Array.isArray(e.policy.collaborators)||!Array.isArray(e.policy.strategies)||e.policy.strategies.some(e=>e.action!==W.Edit))))throw Error(`Invalid object permission batch.`);return e.get(Je).saveMany(t.changes.map(e=>{let n=$t(t.unitId,e.objectId,W.Edit);return{unitId:t.unitId,objectId:e.objectId,objectType:n.type,policy:e.policy}}))}};function Vr(e,t){let n=k(V.deepClone(e)),r=/* @__PURE__ */ new Map;return t.forEach(e=>{var t,i;let a=`recordId`in e?e.recordId:(t=n.rowId)==null?void 0:t[e.row],o=`fieldId`in e?e.fieldId:(i=n.colId)==null?void 0:i[e.col],s=o?n.fields[o]:void 0;if(!a||!o||!s||!n.records[a])return;let c=l(e.value,s),u=f(e.value,c,s,n,a),d=r.get(a)??{};d[o]=u,r.set(a,d)}),[...r].map(([e,t])=>({recordId:e,values:t}))}const Hr={id:`base.command.set-base-range-values`,type:F.COMMAND,handler(e,t){if(!t)return!1;let n=e.get(R),{unitId:r,tableId:a,patches:o}=t,s=n.getUnit(r,H.UNIVER_BASE);if(!s||!Q(s,a))return!1;let c=e.get(I),l=e.get(L),u=s.getSnapshot(),d=k(u.tables[a]),f=o.map(e=>{var t;return`recordId`in e?e.recordId:(t=d.rowId)==null?void 0:t[e.row]}).filter(e=>e!=null),p=o.map(e=>{var t;return`fieldId`in e?e.fieldId:(t=d.colId)==null?void 0:t[e.col]}).filter(e=>e!=null);if(f.length!==o.length||p.length!==o.length||!Q(s,a,{recordIds:f,fieldIds:p})||o.some((e,t)=>!Wn(s,a,p[t],e.value)))return!1;let m=xe(d,Vr(d,o)),h=o.flatMap(e=>{var t,n;if(`row`in e)return[e];let r=(t=d.rowIndex)==null?void 0:t[e.recordId],i=(n=d.colIndex)==null?void 0:n[e.fieldId];return r==null||i==null?[]:[{row:r,col:i,value:e.value}]}),g=new q().setRangeValues(u,a,h),_=new i().invertOp(u,g),v={unitId:r,op:g,trigger:m.length?Hr.id:void 0,hierarchyChanges:m.length?m:void 0,source:m.length?`redo`:void 0},y={unitId:r,op:_,trigger:m.length?Hr.id:void 0,hierarchyChanges:m.length?m.map(e=>({...e,oldParentRecordId:e.parentRecordId,parentRecordId:e.oldParentRecordId})):void 0,source:m.length?`undo`:void 0},b={...v,source:m.length?t.source??`ui`:void 0};return c.syncExecuteCommand(j.id,b)?(n.focusUnit(r),l.pushUndoRedo({unitID:r,undoMutations:[{id:j.id,params:y}],redoMutations:[{id:j.id,params:v}]}),!0):!1}},Ur={id:`base.command.set-base-record-values`,type:F.COMMAND,handler(e,t){if(!t)return!1;let n=e.get(R),{unitId:r,tableId:a,patches:o}=t,s=n.getUnit(r,H.UNIVER_BASE),c=o.map(e=>e.recordId),l=o.flatMap(e=>Object.keys(e.values));if(!s||!Q(s,a,{recordIds:c,fieldIds:l})||o.some(e=>Object.entries(e.values).some(([e,t])=>!Wn(s,a,e,t))))return!1;let u=xe(s.getSnapshot().tables[a],o),d=e.get(I),f=e.get(L),p=e.get(Z).createSetRecordValuesOp(s,a,o),m=new i().invertOp(s.getSnapshot(),p),h={unitId:r,op:p,trigger:u.length?Ur.id:void 0,hierarchyChanges:u.length?u:void 0,source:u.length?t.source??`ui`:void 0},g={...h,source:u.length?`redo`:void 0},_={unitId:r,op:m,trigger:u.length?Ur.id:void 0,hierarchyChanges:u.length?u.map(e=>({...e,oldParentRecordId:e.parentRecordId,parentRecordId:e.oldParentRecordId})):void 0,source:u.length?`undo`:void 0};return d.syncExecuteCommand(j.id,h)?(n.focusUnit(r),f.pushUndoRedo({unitID:r,undoMutations:[{id:j.id,params:_}],redoMutations:[{id:j.id,params:g}]}),!0):!1}},Wr={id:`base.command.set-base-table-hierarchy-field`,type:F.COMMAND,handler:(e,t)=>{if(!t)return!1;let{unitId:n,tableId:r,fieldId:a}=t,o=e.get(R),s=o.getUnit(n,H.UNIVER_BASE);if(!s||!Q(s,r))return!1;let l=s.getSnapshot(),d=l.tables[r];if(a!==null&&!C(d,a))throw new c(`invalidField`);if(u(d)===a)return!0;let f=new q,p=Object.values(d.fields).flatMap(e=>{if(!C(d,e.id))return[];let t=ve(e);if(!t)return[];let n=e.id===a;if(t.relationRole===P.Parent===n)return[];let i={...e.config,targetTableId:t.targetTableId,multiple:t.multiple};return n?i.relationRole=P.Parent:delete i.relationRole,[f.updateField(l,r,e.id,{config:i})]}),m=h(p),g=new i().invertOp(l,m),_={unitId:n,op:m},v={unitId:n,op:g};return e.get(I).syncExecuteCommand(j.id,_)?(o.focusUnit(n),e.get(L).pushUndoRedo({unitID:n,undoMutations:[{id:j.id,params:v}],redoMutations:[{id:j.id,params:_}]}),!0):!1}},Gr={id:`base.command.set-base-view-field-order`,type:F.COMMAND,handler:(e,t)=>{if(!t)return!1;let n=e.get(R),{unitId:r,tableId:a,viewId:o,fieldId:s,target:c}=t,l=n.getUnit(r,H.UNIVER_BASE),u=c.beforeFieldId??c.afterFieldId;if(!l||!u||u===s||!Q(l,a,{viewIds:[o],fieldIds:[s,u]}))return!1;let d=l.getSnapshot().tables[a],f=d.fields[s],p=d.fields[u];if(f.system===!0||f.type===N.RecordId||p.system===!0||p.type===N.RecordId)return!1;let m=e.get(I),h=e.get(L),g=l.getSnapshot(),_=new q().setViewFieldOrder(g,a,o,s,c),v=new i().invertOp(g,_),y={unitId:r,op:_},b={unitId:r,op:v};return m.syncExecuteCommand(j.id,y)?(n.focusUnit(r),h.pushUndoRedo({unitID:r,undoMutations:[{id:j.id,params:b}],redoMutations:[{id:j.id,params:y}]}),!0):!1}},Kr={id:`base.command.set-base-view-field-visible`,type:F.COMMAND,handler(e,t){if(!t)return!1;let n=e.get(R),{unitId:r,tableId:a,viewId:o,fieldId:s,visible:c}=t,l=n.getUnit(r,H.UNIVER_BASE);if(!l||!Q(l,a,{viewIds:[o],fieldIds:[s]}))return!1;let u=l.getSnapshot().tables[a].fields[s];if(u.system===!0||u.type===N.RecordId)return!1;let d=e.get(I),f=e.get(L),p=l.getSnapshot(),m=new q().setViewFieldVisible(p,a,o,s,c),h=new i().invertOp(p,m),g={unitId:r,op:m},_={unitId:r,op:h};return d.syncExecuteCommand(j.id,g)?(n.focusUnit(r),f.pushUndoRedo({unitID:r,undoMutations:[{id:j.id,params:_}],redoMutations:[{id:j.id,params:g}]}),!0):!1}},qr={id:`base.command.set-base-view-field-width`,type:F.COMMAND,handler:(e,t)=>{if(!t)return!1;let n=e.get(R),{unitId:r,tableId:a,viewId:o,fieldId:s,width:c}=t,l=n.getUnit(r,H.UNIVER_BASE);if(!l||!Q(l,a,{viewIds:[o],fieldIds:[s]}))return!1;let u=l.getSnapshot().tables[a].fields[s];if(u.system===!0||u.type===N.RecordId)return!1;let d=e.get(I),f=e.get(L),p=l.getSnapshot(),m=new q().setViewFieldWidth(p,a,o,s,c),h=new i().invertOp(p,m),g={unitId:r,op:m},_={unitId:r,op:h};return d.syncExecuteCommand(j.id,g)?(n.focusUnit(r),f.pushUndoRedo({unitID:r,undoMutations:[{id:j.id,params:_}],redoMutations:[{id:j.id,params:g}]}),!0):!1}},Jr={id:`base.command.set-base-view-filter`,type:F.COMMAND,handler:(e,t)=>{if(!t)return!1;let n=e.get(R),{unitId:r,tableId:a,viewId:o,filter:s}=t,c=n.getUnit(r,H.UNIVER_BASE),l=(s==null?void 0:s.conditions.map(e=>e.fieldId))??[];if(!c||!Q(c,a,{viewIds:[o],fieldIds:l}))return!1;let u=e.get(I),d=e.get(L),f=c.getSnapshot(),p=new q().setViewFilter(f,a,o,s),m=new i().invertOp(f,p),h={unitId:r,op:p},g={unitId:r,op:m};return u.syncExecuteCommand(j.id,h)?(n.focusUnit(r),d.pushUndoRedo({unitID:r,undoMutations:[{id:j.id,params:g}],redoMutations:[{id:j.id,params:h}]}),!0):!1}},Yr={id:`base.command.set-base-view-group`,type:F.COMMAND,handler:(e,t)=>{if(!t)return!1;let n=e.get(R),{unitId:r,tableId:a,viewId:o,group:s}=t,c=n.getUnit(r,H.UNIVER_BASE);if(!c||!Q(c,a,{viewIds:[o],fieldIds:s.map(e=>e.fieldId)}))return!1;let l=e.get(I),u=e.get(L),d=c.getSnapshot(),f=new q().setViewGroup(d,a,o,s),p=new i().invertOp(d,f),m={unitId:r,op:f},h={unitId:r,op:p};return l.syncExecuteCommand(j.id,m)?(n.focusUnit(r),u.pushUndoRedo({unitID:r,undoMutations:[{id:j.id,params:h}],redoMutations:[{id:j.id,params:m}]}),!0):!1}},Xr={id:`base.command.set-base-view-sort`,type:F.COMMAND,handler:(e,t)=>{if(!t)return!1;let n=e.get(R),{unitId:r,tableId:a,viewId:o,sort:s}=t,c=n.getUnit(r,H.UNIVER_BASE);if(!c||!Q(c,a,{viewIds:[o],fieldIds:s.map(e=>e.fieldId)}))return!1;let l=e.get(I),u=e.get(L),d=c.getSnapshot(),f=new q().setViewSort(d,a,o,s),p=new i().invertOp(d,f),m={unitId:r,op:f},h={unitId:r,op:p};return l.syncExecuteCommand(j.id,m)?(n.focusUnit(r),u.pushUndoRedo({unitID:r,undoMutations:[{id:j.id,params:h}],redoMutations:[{id:j.id,params:m}]}),!0):!1}},Zr={id:`base.command.update-base-cell`,type:F.COMMAND,handler(t,n){if(!n)return!1;let r=t.get(R),{unitId:a,tableId:o,recordId:s,fieldId:l,value:u}=n,f=r.getUnit(a,H.UNIVER_BASE);if(!f||!Q(f,o,{recordIds:[s],fieldIds:[l]})||!Wn(f,o,l,u))return!1;let p=f.getSnapshot().tables[o],m=C(p,l)&&d(p).includes(l),h=m?ee(p,l):null,g=(h==null?void 0:h.parentByRecordId.get(s))??null,_=m?le(u)[0]??null:null;if(h){if(_&&!p.records[_])throw new c(`missingRecord`);let t=e(h,s,_);if(!t.valid){let e=t.reason===Me.SelfParent?`selfParent`:t.reason===Me.Cycle?`cycle`:`maxDepth`;throw new c(e)}}let v=t.get(I),y=t.get(L),b=t.get(Z),x;try{x=b.createUpdateCellOp(f,o,s,l,u)}catch(e){return console.error(`Failed to create update cell operation:`,e),!1}let S=new i().invertOp(f.getSnapshot(),x),w=m&&g!==_?[{tableId:o,fieldId:l,recordId:s,oldParentRecordId:g,parentRecordId:_}]:void 0,T={unitId:a,op:x,trigger:w?Zr.id:void 0,hierarchyChanges:w,source:w?n.source??`ui`:void 0},te={...T,source:w?`redo`:void 0},ne={unitId:a,op:S,trigger:w?Zr.id:void 0,hierarchyChanges:w==null?void 0:w.map(e=>({...e,oldParentRecordId:e.parentRecordId,parentRecordId:e.oldParentRecordId})),source:w?`undo`:void 0};return v.syncExecuteCommand(j.id,T)?(r.getFocusedUnit()||r.focusUnit(a),y.pushUndoRedo({unitID:a,undoMutations:[{id:j.id,params:ne}],redoMutations:[{id:j.id,params:te}]}),!0):!1}},Qr={id:`base.command.update-base-field-description`,type:F.COMMAND,handler(e,t){if(!t)return!1;let n=e.get(R),{unitId:r,tableId:a,fieldId:o}=t,s=n.getUnit(r,H.UNIVER_BASE);if(!s||!Q(s,a,{fieldIds:[o]}))return!1;let c=e.get(I),l=e.get(L),u=s.getSnapshot(),d=t.description.trim(),f=new q().updateField(u,a,o,{description:d}),p=new i().invertOp(s.getSnapshot(),f),m={unitId:r,op:f},h={unitId:r,op:p};return c.syncExecuteCommand(j.id,m)?(n.focusUnit(r),l.pushUndoRedo({unitID:r,undoMutations:[{id:j.id,params:h}],redoMutations:[{id:j.id,params:m}]}),!0):!1}},$r={id:`base.command.update-base-field`,type:F.COMMAND,handler:(e,t)=>{if(!t)return!1;let n=e.get(R),{unitId:r,tableId:a,fieldId:o,patch:s}=t,c=n.getUnit(r,H.UNIVER_BASE);if(!c||o===M||!Q(c,a,{fieldIds:[o]}))return!1;let l=e.get(I),u=e.get(L),d=c.getSnapshot();if(!Un(c,a,{...d.tables[a].fields[o],...s}))return!1;let f=new q().updateField(d,a,o,s),p=new i().invertOp(d,f),m={unitId:r,op:f},h={unitId:r,op:p};return l.syncExecuteCommand(j.id,m)?(n.focusUnit(r),u.pushUndoRedo({unitID:r,undoMutations:[{id:j.id,params:h}],redoMutations:[{id:j.id,params:m}]}),!0):!1}},ei={id:`base.command.update-base-kanban-config`,type:F.COMMAND,handler:(e,t)=>{if(!t)return!1;let n=e.get(R),{unitId:r,tableId:a,viewId:o,patch:s}=t,c=n.getUnit(r,H.UNIVER_BASE);if(!c||!Q(c,a,{viewIds:[o]}))return!1;let l=e.get(I),u=e.get(L),d=c.getSnapshot(),f=new q().updateViewConfig(d,a,o,s),p=new i().invertOp(d,f),m={unitId:r,op:f},h={unitId:r,op:p};return l.syncExecuteCommand(j.id,m)?(n.focusUnit(r),u.pushUndoRedo({unitID:r,undoMutations:[{id:j.id,params:h}],redoMutations:[{id:j.id,params:m}]}),!0):!1}},ti={id:`base.command.update-base-record-order`,type:F.COMMAND,handler:(e,t)=>{if(!t)return!1;let n=e.get(R),{unitId:r,tableId:a,recordId:o,orderKey:s}=t,c=n.getUnit(r,H.UNIVER_BASE);if(!c||!Q(c,a,{recordIds:[o]}))return!1;let l=e.get(I),u=e.get(L),d=c.getSnapshot(),f=new q().updateRecordOrder(d,a,o,s),p=new i().invertOp(d,f),m={unitId:r,op:f},h={unitId:r,op:p};return l.syncExecuteCommand(j.id,m)?(n.focusUnit(r),u.pushUndoRedo({unitID:r,undoMutations:[{id:j.id,params:h}],redoMutations:[{id:j.id,params:m}]}),!0):!1}};let ni=/* @__PURE__ */ function(e){return e.Set=`set`,e.Add=`add`,e.Delete=`delete`,e.Clear=`clear`,e}({});const ri={id:`base.command.update-base-view-conditional-color-rules`,type:F.COMMAND,handler:(e,t)=>{var n;if(!t)return!1;let r=e.get(R),a=r.getUnit(t.unitId,H.UNIVER_BASE);if(!a||!Q(a,t.tableId,{viewIds:[t.viewId]}))return!1;let o=a.getSnapshot(),s=o.tables[t.tableId],c=ii(((n=s.views[t.viewId].config.conditionalColoring)==null?void 0:n.rules)??[],t);if(!c)return!1;ai(s,c);let l=new q().updateViewConfig(o,t.tableId,t.viewId,{conditionalColoring:c.length?{rules:c}:null}),u=new i().invertOp(o,l),d={unitId:t.unitId,op:l},f={unitId:t.unitId,op:u};return e.get(I).syncExecuteCommand(j.id,d)?(r.focusUnit(t.unitId),e.get(L).pushUndoRedo({unitID:t.unitId,undoMutations:[{id:j.id,params:f}],redoMutations:[{id:j.id,params:d}]}),!0):!1}};function ii(e,t){switch(t.type){case`set`:return V.deepClone(t.rules);case`add`:return[...V.deepClone(e),V.deepClone(t.rule)];case`delete`:{let n=e.filter(e=>e.id!==t.ruleId);return n.length===e.length?null:V.deepClone(n)}case`clear`:return e.length?[]:null}}function ai(e,t){if(!Array.isArray(t))throw TypeError(`[UpdateBaseViewConditionalColorRulesCommand]: rules must be an array.`);let n=/* @__PURE__ */ new Set;for(let[r,i]of t.entries())si(e,i,oi(i,r,n))}function oi(e,t,n){if(!e||typeof e!=`object`)throw TypeError(`[UpdateBaseViewConditionalColorRulesCommand]: rule at index ${t} must be an object.`);let r=typeof e.id==`string`&&e.id.trim()?`"${e.id}"`:`at index ${t}`;if(typeof e.id!=`string`||!e.id.trim())throw Error(`[UpdateBaseViewConditionalColorRulesCommand]: rule ${r} must have a non-empty id.`);if(n.has(e.id))throw Error(`[UpdateBaseViewConditionalColorRulesCommand]: duplicate rule id "${e.id}".`);n.add(e.id);let i=typeof e.color==`string`&&e.color.trim()?new Fe(e.color):null;if(!(i!=null&&i.isValid))throw Error(`[UpdateBaseViewConditionalColorRulesCommand]: rule ${r} must have a valid CSS color.`);return r}function si(e,t,n){let r=new Set(Object.values(ke)),i=new Set(Object.values(Oe)),a=new Set(Object.values(Ae));if(!r.has(t.target))throw Error(`[UpdateBaseViewConditionalColorRulesCommand]: rule ${n} has unsupported target "${String(t.target)}".`);let o=typeof t.fieldId==`string`?e.fields[t.fieldId]:void 0;if(!o||o.system===!0||o.type===N.RecordId)throw Error(`[UpdateBaseViewConditionalColorRulesCommand]: rule ${n} references missing or non-public field "${String(t.fieldId)}".`);if(!i.has(t.operator))throw Error(`[UpdateBaseViewConditionalColorRulesCommand]: rule ${n} has unsupported operator "${String(t.operator)}".`);if(t.dateMode!==void 0&&!a.has(t.dateMode))throw Error(`[UpdateBaseViewConditionalColorRulesCommand]: rule ${n} has unsupported date mode "${String(t.dateMode)}".`);if(t.target===ke.COLUMN)return;let s=Ce(o.type);if(!s.includes(t.operator))throw Error(`[UpdateBaseViewConditionalColorRulesCommand]: rule ${n} cannot use operator "${t.operator}" with field type "${o.type}". Allowed operators: ${s.join(`, `)}.`);if(t.dateMode!==void 0&&!Ee(o.type))throw Error(`[UpdateBaseViewConditionalColorRulesCommand]: rule ${n} can use dateMode only with Date, CreatedAt, or UpdatedAt fields.`)}const ci={id:`base.command.update-base-view-config`,type:F.COMMAND,handler:(e,t)=>{if(!t)return!1;let n=e.get(R),{unitId:r,tableId:a,viewId:o,patch:s}=t,c=n.getUnit(r,H.UNIVER_BASE);if(!c||!Q(c,a,{viewIds:[o]}))return!1;let l=e.get(I),u=e.get(L),d=c.getSnapshot(),f=new q().updateViewConfig(d,a,o,s),p=new i().invertOp(d,f),m={unitId:r,op:f},h={unitId:r,op:p};return l.syncExecuteCommand(j.id,m)?(n.focusUnit(r),u.pushUndoRedo({unitID:r,undoMutations:[{id:j.id,params:h}],redoMutations:[{id:j.id,params:m}]}),!0):!1}},li={id:`base.command.validate-formula`,type:F.COMMAND,handler(e,t){if(!t)return{valid:!1,reason:`[BaseFormula]: validation parameters are required.`};let n=e.get(R).getUnit(t.unitId,H.UNIVER_BASE),r=n==null?void 0:n.getSnapshot(),i=r==null?void 0:r.tables[t.tableId];return!r||!i?{valid:!1,reason:`[BaseFormula]: table does not exist.`}:new wn().validateFormula(t.formula,i,{snapshot:r})}};let ui=class extends qe{constructor(e){super(e,`BASE_OBJECT_PERMISSION_PLUGIN`,H.UNIVER_BASE,[G.BaseTable,G.BaseField,G.BaseRecord,G.BaseView,G.BaseDashboard])}};ui=D([A(0,He)],ui);const di={id:`base.mutation.set-permission-rule`,type:F.MUTATION,handler:(e,t)=>!t||!e.get(R).getUnit(t.unitId,H.UNIVER_BASE)?!1:e.get(ui).setRule(t.unitId,t.objectType,t.objectId,t.rule)},fi={id:`base.mutation.set-permission-rules`,type:F.MUTATION,handler:(e,t)=>!t||!e.get(R).getUnit(t.unitId,H.UNIVER_BASE)?!1:e.get(ui).setRules(t.unitId,t.rules)};function pi(e){let t=e.now??Date.now(),n=e.primaryFieldId??`${e.id}-field-name`,r=e.gridViewId??`${e.id}-view-grid`,i=e.recordCount??0,a=et(),o={id:n,name:e.primaryFieldName??`Name`,type:N.Text,config:{placeholder:`bases.fieldConfig.textPlaceholder`}},s={},c=[],l={},u={},d={};for(let r=0;r<i;r++){let i=`${e.id}-record-${r+1}`;s[i]={id:i,values:{[M]:i,...e.recordNamePrefix?{[n]:`${e.recordNamePrefix} ${r+1}`}:{}},orderKey:String(r+1).padStart(4,`0`),createdAt:t,updatedAt:t},c.push(i),l[i]=r,u[r]=i,d[r]={0:{v:i,t:Pe.STRING}},e.recordNamePrefix&&(d[r][1]={v:`${e.recordNamePrefix} ${r+1}`,t:Pe.STRING})}let f={id:r,tableId:e.id,name:`Grid`,type:Ne.Grid,fieldOrder:[M,n],fieldSettings:{[M]:{hidden:!0}},config:{frozenFieldCount:1}};return{id:e.id,name:e.name,primaryFieldId:n,fieldOrder:[M,n],fields:{[M]:a,[n]:o},records:s,recordOrder:c,rowIndex:l,rowId:u,colIndex:{[M]:0,[n]:1},colId:{0:M,1:n},cellData:d,views:{[r]:f},viewOrder:[r]}}const mi=`UNIVER_EMBED_BASES_TABLE`;function hi(e){let t=pi({id:e.hostAnchorId,name:e.name??e.embedId,recordCount:0});return{...t,resources:{...t.resources??{},[mi]:vi(e)}}}function gi(e){return{id:j.id,params:{unitId:e.unitId,op:z.type.compose(z.insertOp([`tables`,e.table.id],e.table),z.insertOp([`tableOrder`,e.index??e.snapshot.tableOrder.length],e.table.id)),trigger:`embed-ui`}}}function _i(e){let t=e.snapshot.tableOrder.indexOf(e.tableId);if(!(!e.snapshot.tables[e.tableId]||t<0))return{id:j.id,params:{unitId:e.unitId,op:z.type.compose(z.removeOp([`tables`,e.tableId],e.snapshot.tables[e.tableId]),z.removeOp([`tableOrder`,t],e.tableId)),trigger:`embed-ui`}}}function vi(e){return{version:1,embedId:e.embedId,hostAnchorId:e.hostAnchorId,childUnitId:e.childUnitId,childType:e.childType}}function yi(e){let t=Math.max(0,Number(e.decimalPlaces??0)),n=e.useThousands===!1?`0`:`#,##0`,r=t?`.${`0`.repeat(t)}`:``,i=xi(e.abbreviation);return`${e.currencySymbol?Si(e.currencySymbol):``}${n}${r}${i}`}function bi(e,t){let n=yi(t),r=ct.format(n,e,{throws:!1});return Ci(String(r),t.separatorStyle,t.locale)}function xi(e){return e===`K`?`,"K"`:e===`M`?`,,"M"`:e===`B`?`,,,"B"`:``}function Si(e){return/^[A-Z]+$/i.test(e)?`"${e}"`:e}function Ci(e,t,n){let r=t===`local`?wi(n):t;return r===`periodComma`?e.replace(/,/g,`\0`).replace(/\./g,`,`).replace(/\0/g,`.`):r===`spaceComma`?e.replace(/,/g,` `).replace(/\./g,`,`):r===`spacePeriod`?e.replace(/,/g,` `):e}function wi(e){var t,n;let r=e||new Intl.NumberFormat().resolvedOptions().locale,i;try{i=new Intl.NumberFormat(r,{minimumFractionDigits:2,maximumFractionDigits:2,useGrouping:!0}).formatToParts(1e6)}catch{return`commaPeriod`}let a=(t=i.find(e=>e.type===`group`))==null?void 0:t.value,o=(n=i.find(e=>e.type===`decimal`))==null?void 0:n.value,s=a===`\xA0`||a===` `?` `:a;return s===`.`&&o===`,`?`periodComma`:s===` `&&o===`,`?`spaceComma`:s===` `&&o===`.`?`spacePeriod`:`commaPeriod`}function Ti(e,t){let n=t&&typeof t==`object`&&!Array.isArray(t)?t:{},r=Ei(n.decimalPlaces,0,2,0),i=e.toFixed(r);return n.format===`number`?i:`${i}%`}function Ei(e,t,n,r){if(e==null||e===``)return r;let i=typeof e==`number`?e:Number(e);return Number.isFinite(i)?Math.max(t,Math.min(n,Math.round(i))):r}const Di=[`count`,`filled`,`empty`,`unique`,`sum`,`average`,`min`,`max`],Oi=[`count`,`filled`,`empty`,`unique`],ki=[`sum`,`average`,`min`,`max`],Ai=[`min`,`max`],ji=/* @__PURE__ */ new Set([N.Number,N.Currency,N.Progress,N.Rating]),Mi=/* @__PURE__ */ new Set([N.MultiSelect,N.Person,N.Group]),Ni=`General`;function Pi(e){return typeof e==`string`&&Di.includes(e)}function Fi(e){return!e||typeof e!=`object`?{}:Object.fromEntries(Object.entries(e).filter(e=>Pi(e[1])))}function Ii(e){return ji.has(e.type)?[...Oi,...ki]:e.type===N.Formula?[...Oi,...Zi(e)===`date`?Ai:ki]:Oi}function Li(e,t){return Ii(e).includes(t)}function Ri(e,t,n){if(!Li(e,n))return null;let r=t.filter(t=>!Bi(e,t));switch(n){case`count`:return t.length;case`filled`:return r.length;case`empty`:return t.length-r.length;case`unique`:return new Set(r.map(t=>Wi(e,t))).size;case`sum`:{let t=Vi(e,r);return t.length?Ui(t):null}case`average`:{let t=Vi(e,r);return t.length?Ui(t)/t.length:null}case`min`:{let t=Vi(e,r);return t.length?Math.min(...t):null}case`max`:{let t=Vi(e,r);return t.length?Math.max(...t):null}}}function zi(e,t,n){if(n==null||!Number.isFinite(n))return`-`;if(Oi.includes(t))return String(n);switch(e.type){case N.Number:return bi(n,Ji(e));case N.Currency:return bi(n,Yi(e));case N.Progress:return Ti(n,e.config);case N.Formula:return Xi(n,e);default:return $i(n)}}function Bi(e,t){if(t==null)return!0;if(typeof t==`string`)return t.trim()===``;if(Array.isArray(t))return e.type===N.Attachment?!t.some(qi):t.length===0;if(e.type===N.Link&&typeof t==`object`){let e=t;return[e.text,e.url].every(e=>e==null||String(e).trim()===``)}return!1}function Vi(e,t){let n=[];for(let r of t){let t=Hi(e,r);t!=null&&n.push(t)}return n}function Hi(e,t){if(typeof t==`number`)return Number.isFinite(t)?t:null;if(e.type!==N.Formula&&typeof t==`string`&&t.trim()!==``){let e=Number(t);return Number.isFinite(e)?e:null}return null}function Ui(e){let t=0,n=0;for(let r of e){let e=t+r;n+=Math.abs(t)>=Math.abs(r)?t-e+r:r-e+t,t=e}return t+n}function Wi(e,t){return Mi.has(e.type)&&Array.isArray(t)?Ki([...new Set(t.map(e=>Ki(Gi(e))))].sort()):Ki(Gi(t))}function Gi(e){return Array.isArray(e)?e.map(Gi):e&&typeof e==`object`?Object.fromEntries(Object.entries(e).sort(([e],[t])=>e.localeCompare(t)).map(([e,t])=>[e,Gi(t)])):e}function Ki(e){return JSON.stringify(e)??`undefined`}function qi(e){if(typeof e==`string`)return e.trim()!==``;if(!e||typeof e!=`object`)return!1;let t=e;return[t.id,t.name,t.source,t.url,t.thumbnail].some(e=>e!=null&&String(e).trim()!==``)}function Ji(e){let t=ea(e.config);return{decimalPlaces:na(t.decimalPlaces,0,20,2),separatorStyle:typeof t.separatorStyle==`string`?t.separatorStyle:`local`,locale:typeof t.locale==`string`?t.locale:void 0,useThousands:t.useThousands!==!1,abbreviation:typeof t.abbreviation==`string`?t.abbreviation:`none`}}function Yi(e){let t=ea(e.config);return{...Ji(e),currencySymbol:String(t.currencySymbol??`$`)}}function Xi(e,t){let n=Qi(t);if(n===Ni)return $i(e);try{return String(ct.format(n,e,{throws:!1}))}catch{return $i(e)}}function Zi(e){let t=ea(ea(e.config).numfmt);return typeof t.type==`string`?t.type:`general`}function Qi(e){let t=ea(ea(e.config).numfmt);return String(t.pattern??Ni).trim()||Ni}function $i(e){return String(Number.isInteger(e)?e:Math.round((e+2**-52)*100)/100)}function ea(e){return e&&typeof e==`object`&&!Array.isArray(e)?e:{}}function ta(e){if(e==null||e===``)return null;let t=typeof e==`number`?e:Number(e);return Number.isFinite(t)?t:null}function na(e,t,n,r){let i=ta(e);return i==null?r:Math.max(t,Math.min(n,Math.round(i)))}function ra(e,t){let n=V.deepClone(e),r=t.includeRecords===!0,i=/* @__PURE__ */ new Map,a=t.createViewId??(()=>`view-${nt(8)}`),o=n.viewOrder;return t.regenerateViewIds&&(o=n.viewOrder.map(e=>{let t=a();return i.set(e,t),t})),{...n,id:t.id,name:t.name,records:r?n.records:{},recordOrder:r?n.recordOrder:[],rowIndex:r?n.rowIndex:{},rowId:r?n.rowId:{},cellData:r?n.cellData:{},resources:r?n.resources:{},viewOrder:o,views:Object.fromEntries(Object.entries(n.views??{}).map(([e,n])=>{let r=i.get(e)??(t.regenerateViewIds?a():e);return[r,{...n,id:r,tableId:t.id}]}))}}function ia(e){return new i().getAffectedRanges(e)}function aa(e){return e.patches.map(t=>({unitId:e.unitId,sheetId:e.tableId,range:{startRow:t.row,endRow:t.row,startColumn:t.col,endColumn:t.col}}))}function oa(e,t){return{[e]:Object.fromEntries(t.map(e=>[e,`1`]))}}let sa=class extends Re{constructor(e,t){super(),this._activeDirtyManagerService=e,this._univerInstanceService=t,this._initialize()}_initialize(){this._activeDirtyManagerService.register(j.id,{commandId:j.id,getDirtyData:e=>this._getJson1DirtyData(e.params)}),this.disposeWithMe(ut(()=>{this._activeDirtyManagerService.remove(j.id)}))}_getTable(e,t){let n=this._univerInstanceService.getUnit(e,H.UNIVER_BASE);return(n==null?void 0:n.getSnapshot().tables[t])??null}_getJson1DirtyData(e){if(e.trigger===`formula.mutation.set-formula-calculation-result`)return{};if(!e.unitId||!e.op)return{clearDependencyTreeCache:this._getAllBaseDependencyTreeCacheMap(),forceCalculation:!0};let t=ia(e.op),n=this._getStructureDirtyTableIds(e.unitId,t);if(n.length)return{clearDependencyTreeCache:oa(e.unitId,n),forceCalculation:!0};let r=Object.entries(this._groupCellInvalidations(e.unitId,t)).flatMap(([t,n])=>aa({unitId:e.unitId,tableId:t,patches:n}));return r.length?{dirtyRanges:r}:{}}_getStructureDirtyTableIds(e,t){let n=t.filter(e=>e.reason!==`cell`).flatMap(t=>t.tableId?[t.tableId]:this._getAllTableIds(e));return Array.from(new Set(n))}_groupCellInvalidations(e,t){let n=/* @__PURE__ */ new Set;return t.reduce((t,r)=>{let i=this._toDirtyRangePatch(e,r);if(!i)return t;let a=`${r.tableId}:${i.row}:${i.col}`;return n.has(a)||(n.add(a),t[r.tableId]=t[r.tableId]??[],t[r.tableId].push(i)),t},{})}_toDirtyRangePatch(e,t){if(t.reason!==`cell`)return null;if(t.row!=null&&t.column!=null)return{row:t.row,col:t.column,value:null};if(!t.recordId||!t.fieldId)return null;let n=this._getTable(e,t.tableId);if(!n)return null;let r=m(n,t.recordId,t.fieldId);return r?{row:r.row,col:r.col,value:null}:null}_getAllTableIds(e){let t=this._univerInstanceService.getUnit(e,H.UNIVER_BASE);return Object.values((t==null?void 0:t.getSnapshot().tables)??{}).map(e=>e.id)}_getAllBaseDependencyTreeCacheMap(){let e=this._univerInstanceService.getAllUnitsForType(H.UNIVER_BASE).map(e=>oa(e.getUnitId(),Object.keys(e.getSnapshot().tables)));return Object.assign({},...e)}};sa=D([A(0,pt),A(1,R)],sa);let ca=class extends Re{constructor(e,t,n,r){super(),this._commandService=e,this._univerInstanceService=t,this._formulaCalculationTriggerService=n,this._formulaCalculationSessionService=r,O(this,`_formulaService`,new wn),O(this,`_registeredSuperTableNames`,/* @__PURE__ */ new Map),O(this,`_scheduledSuperTableSyncs`,/* @__PURE__ */ new Set),O(this,`_formulaSyncQueue`,Promise.resolve()),O(this,`_applyingFormulaResults`,!1),this._initialize()}_initialize(){this._univerInstanceService.getAllUnitsForType(H.UNIVER_BASE).forEach(e=>{this._scheduleSuperTableSync(e)}),this.disposeWithMe(this._univerInstanceService.getTypeOfUnitAdded$(H.UNIVER_BASE).subscribe(({unit:e})=>{this._scheduleSuperTableSync(e)})),this.disposeWithMe(this._univerInstanceService.getTypeOfUnitDisposed$(H.UNIVER_BASE).subscribe(e=>{this._enqueueFormulaSync(()=>this._removeSuperTables(e.getUnitId()))})),this.disposeWithMe(this._commandService.onCommandExecuted(e=>this._onCommandExecuted(e)))}_onCommandExecuted(e){if(e.id===ht.id){ua(e.params)&&this._applyFormulaResult(e.params);return}if(this._applyingFormulaResults||e.id!==j.id)return;let t=la(e.params)?e.params:void 0,n=t!=null&&t.unitId?[this._univerInstanceService.getUnit(t.unitId,H.UNIVER_BASE)].filter(e=>!!e):this._univerInstanceService.getAllUnitsForType(H.UNIVER_BASE),r=t==null?void 0:t.op;n.forEach(e=>{let t=r?ia(r):[];(!r||t.some(e=>e.reason!==`cell`))&&this._scheduleSuperTableSync(e)})}_scheduleSuperTableSync(e){let t=e.getUnitId();this._scheduledSuperTableSyncs.has(t)||(this._scheduledSuperTableSyncs.add(t),this._enqueueFormulaSync(async()=>{this._scheduledSuperTableSyncs.delete(t),await this._syncSuperTables(e)}))}async _syncSuperTables(e){let t=e.getUnitId(),n=this._formulaService.getSuperTableRegistrations(e.getSnapshot()),r=new Set(n.map(({tableName:e})=>e)),i=this._registeredSuperTableNames.get(t)??/* @__PURE__ */ new Set;for(let e of i)if(!r.has(e)&&!await this._executeSuperTableMutationWhileActive(mt.id,{unitId:t,tableName:e}))return;for(let{tableName:e,reference:r}of n)if(!await this._executeSuperTableMutationWhileActive(gt.id,{unitId:t,tableName:e,reference:r}))return;this._registeredSuperTableNames.set(t,r)}async _removeSuperTables(e){let t=this._registeredSuperTableNames.get(e)??/* @__PURE__ */ new Set;for(let n of t)if(!await this._executeSuperTableMutationWhileActive(mt.id,{unitId:e,tableName:n}))return;this._registeredSuperTableNames.delete(e)}async _executeSuperTableMutationWhileActive(e,t){return!this._disposed&&await this._commandService.executeCommand(e,t,{onlyLocal:!0})&&!this._disposed}_enqueueFormulaSync(e){this._formulaSyncQueue=this._formulaSyncQueue.then(e,e).catch(()=>void 0)}_applyFormulaResult(e){let t=this._univerInstanceService.getAllUnitsForType(H.UNIVER_BASE);if(!t.some(t=>{var n;return((n=e.unitData)==null?void 0:n[t.getUnitId()])!=null}))return;let n=t.flatMap(t=>Tn(t.getSnapshot(),e));if(!n.length){this._formulaCalculationSessionService.markResultApplied(Ct.BASE,e);return}let r=new q,i=n.flatMap(e=>aa({unitId:e.unitId,tableId:e.tableId,patches:e.patches}));this._applyingFormulaResults=!0,(async()=>{for(let e of n){let t=this._univerInstanceService.getUnit(e.unitId,H.UNIVER_BASE);t&&await this._commandService.executeCommand(j.id,{unitId:e.unitId,op:r.setRangeValues(t.getSnapshot(),e.tableId,e.patches),trigger:ht.id},{onlyLocal:!0})}await this._commandService.executeCommand(_t.id,{dirtyRanges:i},{onlyLocal:!0})})().finally(()=>{this._applyingFormulaResults=!1,this._formulaCalculationSessionService.markResultApplied(Ct.BASE,e)})}};ca=D([A(0,I),A(1,R),A(2,Ue(ft)),A(3,Ue(St))],ca);function la(e){return typeof e==`object`&&!!e&&`unitId`in e&&typeof e.unitId==`string`&&`op`in e&&Array.isArray(e.op)}function ua(e){return typeof e==`object`&&!!e&&`unitData`in e&&`unitOtherData`in e}const da=/* @__PURE__ */ new Set([zr.id,Br.id,li.id]);let fa=class extends Re{constructor(e,t,n,r){super(),this._injector=e,this._commandService=t,this._permissionService=n,this._instanceService=r,this._instanceService.getAllUnitsForType(H.UNIVER_BASE).forEach(e=>this._registerUnitPermissionPoints(e.getUnitId())),this.disposeWithMe(this._instanceService.getTypeOfUnitAdded$(H.UNIVER_BASE).subscribe(({unit:e})=>this._registerUnitPermissionPoints(e.getUnitId()))),this.disposeWithMe(this._commandService.beforeCommandExecuted((e,t)=>this._check(e,t))),this.disposeWithMe(this._instanceService.getTypeOfUnitDisposed$(H.UNIVER_BASE).subscribe(e=>{this._injector.get(Je).clearUnit(e.getUnitId()),rn(this._permissionService,e.getUnitId())}))}_registerUnitPermissionPoints(e){Wt.forEach(t=>{let n=$t(e,e,t);this._permissionService.getPermissionPoint(n.id)||this._permissionService.addPermissionPoint(n)}),this._injector.get(Je).initializeUnit({unitId:e,objectId:e,objectType:G.Base})}_check(e,t){var n;if(t!=null&&t.fromCollab||t!=null&&t.fromChangeset)return;let r=wa(e.params)?e.params:void 0;if(t!=null&&t.onlyLocal&&e.id===`base.mutation.apply-base-json1`&&(r==null?void 0:r.trigger)===ht.id)return;let i=ma(e.id),a=pa(r==null?void 0:r.unitId,t)??(i?(n=this._instanceService.getCurrentUnitOfType(H.UNIVER_BASE))==null?void 0:n.getUnitId():void 0);if(!a||this._instanceService.getUnitType(a)!==H.UNIVER_BASE)return;if(i){if(!en(this._permissionService,a,a,i))throw this._injector.get(ze).emitPermissionDenied(a),new Ie(`Base ${W[i]} permission denied.`);return}if(da.has(e.id)||!e.id.startsWith(`base.command.`)&&!e.id.startsWith(`base.mutation.`))return;let o=this._instanceService.getUnit(a,H.UNIVER_BASE),s=o?ha(o,r??{},e.id):[];if(!o||!an(this._permissionService,a,s))throw this._injector.get(ze).emitPermissionDenied(a,s),new Ie(`Base edit permission denied.`)}};fa=D([A(0,Ue(We)),A(1,I),A(2,Ve),A(3,R)],fa);function pa(e,t){return typeof e==`string`?e:typeof(t==null?void 0:t.unitId)==`string`?t.unitId:void 0}function ma(e){if(e===`bases-exchange-client.operation.export-base`||e===`bases-exchange-client.operation.export-base-by-format`)return W.Export;if(e.startsWith(`thread-comment.command.`)||e.startsWith(`thread-comment.mutation.`))return W.Comment}function ha(e,t,n){if(n===`base.mutation.apply-base-json1`&&Ca(t.op))return ga(e,t.op);let r=/* @__PURE__ */ new Set,i=ba(t,`table`),a=typeof t.tableId==`string`?t.tableId:i[0];if(i.forEach(e=>r.add(J(e))),!a)return[...r];let o=e.getSnapshot().tables[a],s=new Set(ba(t,`field`)),c=new Set(ba(t,`record`)),l=new Set(ba(t,`view`));return xa(o,t.patches,c,s),Sa(t,s),s.forEach(e=>r.add(Y(a,e))),c.forEach(e=>r.add(X(a,e))),l.forEach(e=>r.add(Zt(a,e))),[...r]}function ga(e,t){let n=e.getSnapshot(),r=new i().getAffectedRanges(t),a=_a(t),o=/* @__PURE__ */ new Set;return r.forEach(e=>{var t,r;let{tableId:i}=e;if(!i)return;o.add(J(i));let s=n.tables[i],c=e.recordId??(e.row==null||s==null||(t=s.rowId)==null?void 0:t[e.row]),l=e.fieldId??(e.column==null||s==null||(r=s.colId)==null?void 0:r[e.column]);c&&o.add(X(i,c)),l&&o.add(Y(i,l)),e.viewId&&o.add(Zt(i,e.viewId)),(e.reason===`table`||a.has(i))&&s&&(Object.keys(s.fields).forEach(e=>o.add(Y(i,e))),Object.keys(s.records).forEach(e=>o.add(X(i,e))),Object.keys(s.views).forEach(e=>o.add(Zt(i,e)))),e.reason===`record`&&s&&Object.keys(s.fields).forEach(e=>o.add(Y(i,e)))}),r.length===0&&ya(t,`tableOrder`)&&Object.keys(n.tables).forEach(e=>o.add(J(e))),[...o]}function _a(e){let t=/* @__PURE__ */ new Set,n=e=>{Array.isArray(e)&&e.forEach((r,i)=>{r===`tables`&&typeof e[i+1]==`string`&&va(e[i+2])&&t.add(e[i+1]),n(r)})};return n(e),t}function va(e){return!!e&&typeof e==`object`&&!Array.isArray(e)&&(`i`in e||`r`in e||`p`in e||`d`in e||`es`in e||`ena`in e||`et`in e)}function ya(e,t){return Array.isArray(e)&&e.some(e=>e===t||ya(e,t))}function ba(e,t){let n=/* @__PURE__ */ new Set,r=`${t}Id`,i=`${t}Ids`,a=(e,o=``)=>{typeof e==`string`?o===r&&n.add(e):Array.isArray(e)?o===i?e.forEach(e=>{typeof e==`string`&&n.add(e)}):e.forEach(e=>a(e,o)):wa(e)&&Object.entries(e).forEach(([e,r])=>{e===`id`&&o===t&&typeof r==`string`?n.add(r):a(r,e)})};return a(e),[...n]}function xa(e,t,n,r){e&&Array.isArray(t)&&t.forEach(t=>{if(!wa(t))return;let i=t;if(typeof i.row==`number`){var a;let t=(a=e.rowId)==null?void 0:a[i.row];t&&n.add(t)}if(typeof i.col==`number`){var o;let t=(o=e.colId)==null?void 0:o[i.col];t&&r.add(t)}})}function Sa(e,t,n=``){e&&typeof e==`object`&&(Array.isArray(e)?e.forEach(e=>Sa(e,t,n)):wa(e)&&Object.entries(e).forEach(([e,r])=>{n===`values`&&t.add(e),Sa(r,t,e)}))}function Ca(e){return Array.isArray(e)}function wa(e){return typeof e==`object`&&!!e&&!Array.isArray(e)}const Ta=tt(`base.resource.service`);let Ea=class{constructor(e){this._univerInstanceService=e}getBase(e){return this._univerInstanceService.getUnit(e,H.UNIVER_BASE)??null}getAllBases(){return this._univerInstanceService.getAllUnitsForType(H.UNIVER_BASE)}createBase(e){return this._univerInstanceService.createUnit(H.UNIVER_BASE,e)}};Ea=D([A(0,R)],Ea);const Da=tt(`base.view-registry.service`);var Oa=class extends Re{constructor(...e){super(...e),O(this,`_plugins`,/* @__PURE__ */ new Map)}register(e){return this._plugins.set(e.type,e),ut(()=>this._plugins.delete(e.type))}get(e){let t=this._plugins.get(e);if(!t)throw Error(`[BaseViewRegistryService]: view type "${e}" is not registered.`);return t}getAll(){return Array.from(this._plugins.values())}};const ka=[j];let Aa=class extends Xe{constructor(e=_e,t,n,r,i,a){super(),this._config=e,this._injector=t,this._commandService=n,this._univerInstanceService=r,this._configService=i,this._dataSyncPrimaryController=a,O(this,`_baseSyncDisposables`,/* @__PURE__ */ new Map);let{...o}=ot({},_e,this._config);this._configService.setConfig(be,o),this._univerInstanceService.registerCtorForType(H.UNIVER_BASE,je),this._initDependencies(),this._initCommands(),this._initRegistries(),this._initDataSync()}onStarting(){this._injector.add([ui]),this.disposeWithMe(this._injector.get(I).registerCommand(di)),this.disposeWithMe(this._injector.get(I).registerCommand(fi)),this.disposeWithMe(this._injector.get(Je).registerRuleModel(G.Base,this._injector.get(ui),di.id,fi.id))}onReady(){dt(this._injector,[[Ea],[fn],[sa]]),this._univerInstanceService.getAllUnitsForType(H.UNIVER_BASE).forEach(e=>this._syncBaseUnit(e.getUnitId())),dt(this._injector,[[ca],[fa]])}_initDependencies(){[[ce,{useClass:fe}],[Da,{useClass:Oa}],[Se,{useClass:de}],[Ta,{useClass:Ea}],[on,{useClass:sn}],[Z,{useClass:fn}],[sa],[ca],[fa]].forEach(e=>this._injector.add(e))}_initCommands(){[ir,Fr,Sr,Zr,Ur,Hr,er,Qn,Yn,xr,Xn,Tr,ti,Ar,$n,wr,$r,Qr,Zn,br,kr,li,yr,Er,Ir,Rr,Cr,Pr,ci,ri,Lr,zr,Br,ei,Jr,Xr,Yr,Wr,Kr,qr,Gr,...ka].forEach(e=>this.disposeWithMe(this._commandService.registerCommand(e))),ka.forEach(e=>{var t;return(t=this._dataSyncPrimaryController)==null?void 0:t.registerSyncingMutations(e)})}_initDataSync(){this._dataSyncPrimaryController&&(this.disposeWithMe(this._univerInstanceService.getTypeOfUnitAdded$(H.UNIVER_BASE).subscribe(({unit:e})=>{this._syncBaseUnit(e.getUnitId())})),this.disposeWithMe(this._univerInstanceService.getTypeOfUnitDisposed$(H.UNIVER_BASE).subscribe(e=>{var t;(t=this._baseSyncDisposables.get(e.getUnitId()))==null||t.dispose(),this._baseSyncDisposables.delete(e.getUnitId())})))}_syncBaseUnit(e){this._dataSyncPrimaryController&&!this._baseSyncDisposables.has(e)&&this._baseSyncDisposables.set(e,this._dataSyncPrimaryController.syncUnit(e))}_initRegistries(){let e=this._injector.get(ce);te().forEach(t=>e.register(t));let t=this._injector.get(Da),n=this._injector.get(Se);oe().forEach(e=>{t.register(e),n.registerView(e)})}};O(Aa,`pluginName`,`BASES_PLUGIN`),O(Aa,`packageName`,ne),O(Aa,`version`,me),O(Aa,`type`,H.UNIVER_BASE),Aa=D([Le(bt,vt),A(1,Ue(We)),A(2,I),A(3,R),A(4,Be),A(5,Ye(xt))],Aa);function ja(e){let t=Wa(e.query);if(!t)return{query:``,total:0,matches:[]};let n=[],r=[];for(let t=0;t<e.fieldIds.length;t++){if(!(t in e.fieldIds))continue;let n=e.table.fields[e.fieldIds[t]];n&&r.push({field:n,fieldIndex:t})}e.rows.forEach((e,i)=>{r.forEach(({field:r,fieldIndex:a})=>{let o=Ma(e.values[r.id],r);if(!o)return;let s=Ua(t,o.searchTexts);s<=0||n.push({recordId:e.recordId,fieldId:r.id,fieldType:r.type,text:o.displayText,score:s,rowIndex:i,fieldIndex:a})})}),n.sort((e,t)=>t.score-e.score||e.rowIndex-t.rowIndex||e.fieldIndex-t.fieldIndex);let i=e.limit==null?n:n.slice(0,Math.max(0,e.limit));return{query:t,total:n.length,matches:Ka(i)}}function Ma(e,t){let n=[];for(let r of Pa(e,t)){let e=Wa(r);e&&n.push(e)}return n.length?{displayText:Na(e,t),searchTexts:Ga(n)}:null}function Na(e,t){switch(t.type){case N.SingleSelect:case N.MultiSelect:case N.Group:return Fa(e,t).join(`, `);case N.Link:return Ra(e);case N.Date:case N.CreatedAt:case N.UpdatedAt:return za(e,t);case N.Number:case N.Currency:return Ba(e,t);case N.Progress:return Va(e,t);case N.Checkbox:return e?`Checked`:`Unchecked`;case N.Attachment:return Ha(e).join(`, `);default:return $(e)}}function Pa(e,t){switch(t.type){case N.SingleSelect:case N.MultiSelect:case N.Group:{let n=Fa(e,t);if(Array.isArray(e)){let t=[...n];for(let n=0;n<e.length;n++)n in e&&t.push(String(e[n]));return t}return e==null?n:[...n,String(e)]}case N.Link:{let t=La(e);return[t.text,t.url]}case N.Date:case N.CreatedAt:case N.UpdatedAt:return[za(e,t),$(e)];case N.Number:case N.Currency:return[Ba(e,t),$(e)];case N.Progress:return[Va(e,t),$(e)];case N.Checkbox:return e?[`Checked`,`Yes`,`True`]:[`Unchecked`,`No`,`False`];case N.Attachment:return Ha(e);default:return[$(e)]}}function Fa(e,t){var n;let r=Array.isArray((n=t.config)==null?void 0:n.options)?t.config.options:[],i=[];if(Array.isArray(e)){for(let t=0;t<e.length;t++){if(!(t in e))continue;let n=String(e[t]);n&&i.push(Ia(r,n))}return i}if(e!=null){let t=String(e);t&&i.push(Ia(r,t))}return i}function Ia(e,t){for(let n of e){if(typeof n==`string`&&n===t)return n;if(n&&typeof n==`object`){let e=n;if(String(e.id??e.name??``)===t||String(e.name??``)===t)return String(e.name??e.id??t)}}return t}function La(e){if(e&&typeof e==`object`&&!Array.isArray(e)){let t=e;return{text:String(t.text??t.url??``),url:String(t.url??``)}}return{text:$(e),url:$(e)}}function Ra(e){let t=La(e);return t.text||t.url}function za(e,t){return typeof e!=`number`||!Number.isFinite(e)?``:r(e,t.config)}function Ba(e,t){let n=typeof e==`number`?e:Number(e);return Number.isFinite(n)?bi(n,t.config):``}function Va(e,t){let n=typeof e==`number`?e:Number(e);return Number.isFinite(n)?Ti(n,t.config):``}function Ha(e){return Array.isArray(e)?e.flatMap(e=>{if(!e||typeof e!=`object`)return[];let t=e,n=String(t.name??t.url??``).trim();return n?[n]:[]}):[]}function $(e){if(e==null)return``;if(typeof e==`boolean`)return e?`true`:`false`;if(typeof e==`number`)return String(e);if(Array.isArray(e)){let t=[];for(let n=0;n<e.length;n++){if(!(n in e))continue;let r=$(e[n]);r&&t.push(r)}return t.join(`, `)}if(typeof e==`object`){let t=e;return String(t.title??t.name??t.text??t.url??``).trim()}return String(e)}function Ua(e,t){let n=0;for(let r of t)r&&(r===e?n=Math.max(n,300):r.startsWith(e)?n=Math.max(n,250):r.includes(e)&&(n=Math.max(n,200)));return n}function Wa(e){return e.trim().replace(/\s+/g,` `).toLowerCase()}function Ga(e){return Array.from(new Set(e))}function Ka(e){let t=[];for(let{rowIndex:n,fieldIndex:r,...i}of e)t.push(i);return t}function qa(e){return{compress:!0,kind:`base-range-values-v1`,u:e.unitId,t:e.tableId,p:e.patches.map(e=>[e.row,e.col,Za(e.value)]),...e.trigger===void 0?{}:{g:e.trigger}}}function Ja(e){if(io(e,`base-range-values-v1`),!Ya(e)){if(!ro(e))throw Error(`[BaseCompression]: invalid uncompressed range values payload.`);return e}return{unitId:e.u,tableId:e.t,patches:e.p.map(([e,t,n])=>({row:e,col:t,value:Qa(n)})),...e.g===void 0?{}:{trigger:e.g}}}function Ya(e){return!ao(e)||e.compress!==!0||e.kind!==`base-range-values-v1`?!1:typeof e.u==`string`&&typeof e.t==`string`&&(e.g===void 0||typeof e.g==`string`)&&Array.isArray(e.p)&&e.p.every(e=>Array.isArray(e)&&e.length===3&&Number.isInteger(e[0])&&Number.isInteger(e[1]))}function Xa(e){return!ao(e)||e.compress!==!0||e.kind!==`base-snapshot-v1`||!ao(e.tables)?!1:Object.values(e.tables).every(e=>ao(e)&&(e.cd===void 0||Array.isArray(e.cd)&&e.cd.every(e=>Array.isArray(e)&&e.length===3&&Number.isInteger(e[0])&&Number.isInteger(e[1])&&no(e[2]))))}function Za(e){return to(e)?$a(e):e}function Qa(e){return no(e)?eo(e):e}function $a(e){return{c:1,...e.v===void 0?{}:{v:e.v},...e.t!==void 0&&e.t!==null?{t:e.t}:e.t===null?{t:null}:{},...e.f!==void 0&&e.f!==null?{f:e.f}:e.f===null?{f:null}:{},...e.si!==void 0&&e.si!==null?{si:e.si}:e.si===null?{si:null}:{},...e.p!==void 0&&e.p!==null?{p:e.p}:e.p===null?{p:null}:{}}}function eo(e){return{...e.v===void 0?{}:{v:e.v},...e.t===void 0?{}:{t:e.t},...e.f===void 0?{}:{f:e.f},...e.si===void 0?{}:{si:e.si},...e.p===void 0?{}:{p:e.p}}}function to(e){return!!e&&typeof e==`object`&&!Array.isArray(e)&&(Object.prototype.hasOwnProperty.call(e,`v`)||Object.prototype.hasOwnProperty.call(e,`t`)||Object.prototype.hasOwnProperty.call(e,`p`)||Object.prototype.hasOwnProperty.call(e,`f`)||Object.prototype.hasOwnProperty.call(e,`si`))}function no(e){return ao(e)&&e.c===1}function ro(e){return ao(e)&&typeof e.unitId==`string`&&typeof e.tableId==`string`&&(e.trigger===void 0||typeof e.trigger==`string`)&&Array.isArray(e.patches)&&e.patches.every(e=>ao(e)&&Number.isInteger(e.row)&&Number.isInteger(e.col)&&`value`in e)}function io(e,t){if(ao(e)&&e.compress===!0){if(e.kind!==t)throw Error(`[BaseCompression]: unsupported compressed payload kind "${String(e.kind)}".`);if(!(t===`base-range-values-v1`?Ya(e):Xa(e)))throw Error(`[BaseCompression]: invalid compressed payload "${t}".`)}}function ao(e){return!!e&&typeof e==`object`&&!Array.isArray(e)}export{j as ApplyBaseJson1Mutation,Di as BASE_FIELD_STAT_TYPES,De as BASE_HIERARCHY_MAX_LEVELS,n as BASE_HIERARCHY_PARENT_FIELD_ID,tr as BASE_TABLE_NAME_REQUIREMENTS,Wt as BASE_UNIT_PERMISSION_ACTIONS,ni as BaseConditionalColorRuleUpdateType,S as BaseEventSource,wn as BaseFormulaService,c as BaseHierarchyError,x as BaseHierarchyErrorCode,ae as BaseHistoryAction,i as BaseJson1OpApplier,q as BaseJson1OpFactory,Yn as BatchCreateBaseRecordsCommand,Xn as BatchDeleteBaseRecordsCommand,Zn as ChangeBaseFieldTypeCommand,Qn as CreateBaseChildRecordCommand,$n as CreateBaseFieldCommand,er as CreateBaseRecordCommand,ir as CreateBaseTableCommand,yr as CreateBaseViewCommand,br as DeleteBaseFieldCommand,xr as DeleteBaseRecordCommand,Sr as DeleteBaseTableCommand,Cr as DeleteBaseViewCommand,wr as DuplicateBaseFieldCommand,Tr as DuplicateBaseRecordCommand,mi as EMBED_BASES_TABLE_RESOURCE_KEY,Er as EnsureBaseTemporalViewDateFieldsCommand,Z as IBaseCommandService,on as IBasePermissionService,Se as IBaseProjectionService,Da as IBaseViewRegistryService,kr as MoveBaseFieldCommand,Ar as MoveBaseHierarchyRecordCommand,Pr as MoveBaseViewCommand,Fr as RenameBaseTableCommand,Ir as RenameBaseViewCommand,Lr as SetBaseKanbanGroupFieldCommand,Rr as SetBaseNameCommand,zr as SetBasePermissionCommand,di as SetBasePermissionRuleMutation,fi as SetBasePermissionRulesMutation,Br as SetBasePermissionsCommand,Hr as SetBaseRangeValuesCommand,Ur as SetBaseRecordValuesCommand,Wr as SetBaseTableHierarchyFieldCommand,Gr as SetBaseViewFieldOrderCommand,Kr as SetBaseViewFieldVisibleCommand,qr as SetBaseViewFieldWidthCommand,Jr as SetBaseViewFilterCommand,Yr as SetBaseViewGroupCommand,Xr as SetBaseViewSortCommand,Aa as UniverBasesPlugin,Zr as UpdateBaseCellCommand,$r as UpdateBaseFieldCommand,Qr as UpdateBaseFieldDescriptionCommand,ei as UpdateBaseKanbanConfigCommand,ti as UpdateBaseRecordOrderCommand,ri as UpdateBaseViewConditionalColorRulesCommand,ci as UpdateBaseViewConfigCommand,li as ValidateBaseFormulaCommand,Te as baseDateSerialToDateMs,ee as buildBaseHierarchyIndex,Ri as calculateBaseFieldStat,an as canEditBaseTargets,qa as compressBaseRangeValuesPayload,ra as copyBaseTableSnapshot,s as createBaseHierarchyParentField,pi as createDefaultBaseTableSnapshot,hi as createEmbedBasesTable,gi as createEmbedBasesTableAddMutation,_i as createEmbedBasesTableRemoveMutation,rr as createUniqueBaseTableName,y as dateToExcelSerial,Ja as decompressBaseRangeValuesPayload,k as ensureBaseTableCellLayout,pe as findRecordLinkReferences,r as formatBaseDateValue,zi as formatBaseFieldStatValue,bi as formatBaseNumberValue,Ti as formatBaseProgressValue,d as getActiveBaseHierarchyFieldIds,p as getBaseCellFormulaValue,g as getBaseCellValue,Ce as getBaseConditionalColorOperators,Qt as getBaseDashboardPermissionObjectId,Y as getBaseFieldPermissionObjectId,rt as getBaseFormulaTableName,ge as getBaseHierarchyDeletePromotions,u as getBaseHierarchyFieldId,en as getBasePermissionValue,X as getBaseRecordPermissionObjectId,J as getBaseTablePermissionObjectId,Zt as getBaseViewPermissionObjectId,ve as getRecordLinkFieldConfig,Ii as getSupportedBaseFieldStatTypes,w as getVisibleBaseHierarchyRows,wi as inferBaseSeparatorStyle,Ee as isBaseConditionalColorDateField,Bi as isBaseFieldStatEmptyValue,Li as isBaseFieldStatSupported,Pi as isBaseFieldStatType,C as isBaseHierarchyField,Ya as isCompressedBaseRangeValuesPayload,ie as matchesBaseCondition,we as normalizeBaseDateFieldConfig,b as normalizeBaseDateSerial,Fi as normalizeBaseFieldStatsConfig,le as parseRecordLinkIds,T as projectBaseHierarchy,a as resolveBaseDateFieldConfig,_ as resolveBaseHierarchyFieldId,Vr as resolveBaseRangeHierarchyPatches,ja as searchBaseTable,re as serializeRecordLinkIds,tn as setBasePermissionValue,v as traceBasePerformance,ar as uniqueFieldName,e as validateBaseHierarchyMove,se as validateBaseHierarchyRecordCreations,xe as validateBaseHierarchyRecordPatches,nr as validateBaseTableName,he as validateCanonicalRecordLinkValue,ye as validateRecordLinkField,E as validateRecordLinkValue};
|
|
1
|
+
import{$ as e,A as t,B as n,C as r,D as i,E as a,F as o,G as s,H as c,I as l,J as u,K as d,L as f,M as p,N as m,O as h,P as g,Q as _,R as v,S as y,T as b,U as x,V as S,W as C,X as w,Y as ee,Z as T,_ as E,a as te,at as ne,b as D,c as re,ct as ie,d as ae,dt as oe,et as se,f as ce,ft as le,g as O,h as ue,it as de,j as k,k as fe,l as pe,lt as me,m as he,mt as ge,nt as _e,o as ve,ot as ye,p as be,pt as xe,q as Se,r as Ce,rt as we,s as A,st as Te,t as Ee,tt as De,u as Oe,ut as ke,v as Ae,w as je,x as Me,y as Ne,z as Pe}from"./config-DtzarxLi.js";import{BASE_RECORD_ID_FIELD_ID as j,BaseConditionalColorOperator as Fe,BaseConditionalColorTarget as Ie,BaseConditionalDateMode as Le,BaseDataModel as Re,BaseFieldType as M,BaseHierarchyInvalidReason as ze,BaseRecordLinkRole as N,BaseViewType as Be,CellValueType as Ve,ColorKit as He,CommandType as P,CustomCommandExecutionError as Ue,DependentOn as We,Disposable as Ge,ErrorService as Ke,ICommandService as F,IConfigService as qe,IPermissionService as Je,IResourceManagerService as Ye,IUndoRedoService as I,IUniverInstanceService as L,Inject as Xe,Injector as Ze,JSON1 as R,JSONX as Qe,ObjectMatrix as $e,ObjectPermissionRuleModel as et,ObjectPermissionService as tt,Optional as nt,PermissionStatus as z,Plugin as rt,Tools as B,UniverInstanceType as V,allocateBaseFormulaTableName as it,assertBaseTableRecordIdentity as at,createBaseFormulaTableNameMap as ot,createBaseRecordIdField as st,createIdentifier as ct,generateRandomId as lt,getBaseFormulaTableName as ut,isBaseRecordIdFieldName as dt,isValidBaseRecordId as ft,merge as pt,nameCharacterCheck as mt,numfmt as ht,regexp as H,sequenceExecute as gt,toDisposable as _t,touchDependencies as vt}from"@univerjs/core";import{FormulaCalculationTriggerService as yt,IActiveDirtyManagerService as bt,RemoveSuperTableMutation as xt,SetFormulaCalculationResultMutation as St,SetSuperTableMutation as Ct,SetTriggerFormulaCalculationStartMutation as wt,UniverProFormulaEnginePlugin as Tt,refactorFormulaUnitQualifier as Et}from"@univerjs-pro/engine-formula";import{UnitAction as U,UnitObject as W}from"@univerjs/protocol";import{UniverLicensePlugin as Dt}from"@univerjs-pro/license";import{DataSyncPrimaryController as Ot}from"@univerjs/rpc";import{FormulaCalculationSessionService as kt,FormulaResultApplicationType as At}from"@univerjs/engine-formula";const G={table:e=>[`tables`,e],field:(e,t)=>[`tables`,e,`fields`,t],fieldOrder:(e,t)=>[`tables`,e,`fieldOrder`,t],record:(e,t)=>[`tables`,e,`records`,t],cell:(e,t,n)=>[`tables`,e,`records`,t,`values`,n],cellData:(e,t,n)=>[`tables`,e,`cellData`,String(t),String(n)],attachmentSet:(e,t)=>[`tables`,e,`resources`,`attachmentSets`,t],attachment:(e,t)=>[`tables`,e,`resources`,`attachments`,t],view:(e,t)=>[`tables`,e,`views`,t],viewFieldHidden:(e,t,n)=>[`tables`,e,`views`,t,`fieldSettings`,n,`hidden`]};var K=class{setBaseName(e,t){let n=this.renameFormulaUnitReferences(e,e.name,t);return p([this._replaceOptional([`name`],e.name,t),...n?[n]:[]])}renameFormulaUnitReferences(e,t,n){let r=Object.values(e.tables).flatMap(e=>Object.values(e.fields).flatMap(r=>{var i,a;if(r.type!==M.Formula)return[];let o=String(((i=r.config)==null?void 0:i.formula)??``),s=Et(o,t,n);return s===o?[]:[this._replaceOptional([...G.field(e.id,r.id),`config`,`formula`],(a=r.config)==null?void 0:a.formula,s)]}));return r.length>0?p(r):void 0}createTable(e,t,n=e.tableOrder.length){let r=it(t.name,ot(e).values(),t.formulaName),i=t.formulaName===r?t:{...t,formulaName:r};return p([...this._materializeFormulaTableNames(e),R.insertOp(G.table(t.id),i),R.insertOp([`tableOrder`,n],t.id)])}renameTable(e,t,n){let r=e.tables[t];return p([...this._materializeFormulaTableNames(e),R.replaceOp([...G.table(t),`name`],r.name,n)])}deleteTable(e,t){let n=e.tables[t],r=e.tableOrder.indexOf(t);return p([...this._materializeFormulaTableNames(e,/* @__PURE__ */ new Set([t])),R.removeOp(G.table(t),n),R.removeOp([`tableOrder`,r],t)])}createRecord(e,t,n){return this.createRecords(e,t,[n])}createRecords(e,t,n){let r=e.tables[t],i=n.filter(e=>e&&!r.records[e.id]).map(e=>({...e,values:{...e.values,[j]:e.id}}));if(!i.length)return R.replaceOp([...G.table(t),`records`],r.records,r.records);let a=new R.WriteCursor,o=[...i].sort((e,t)=>e.id<t.id?-1:+(e.id>t.id)),s=[...G.table(t),`records`];a.descendPath(s);for(let e of o)a.descend(e.id),a.write(`i`,e),a.ascend();for(let e=0;e<s.length;e+=1)a.ascend();return p([a.get(),this._createRecordOrderOp(e,t,i)])}deleteRecord(e,t,n){return this.deleteRecords(e,t,[n])}deleteRecords(e,t,n){let r=e.tables[t],i=Array.from(new Set(n)).filter(e=>r.records[e]);if(!i.length)return R.replaceOp([...G.table(t),`records`],r.records,r.records);let a=new Set(i),o=T(r,i).map(n=>this.updateCell(e,t,n.recordId,n.fieldId,``));return p([this._deleteRecordsBaseOp(e,t,i,a),...o])}createDeleteRecordsUndoOp(e,t,n,r){let i=e.tables[n],a=Array.from(new Set(r)).filter(e=>i.records[e]),o=new Set(a),s=new k().invertOp(e,this._deleteRecordsBaseOp(e,n,a,o)),c=T(i,a).map(e=>this.updateCell(t,n,e.recordId,e.fieldId,e.oldParentRecordId??``));return p([s,...c])}_deleteRecordsBaseOp(e,t,n,r){let i=e.tables[t],a=n.map(e=>{var t;return{recordId:e,index:((t=i.recordOrder)==null?void 0:t.indexOf(e))??-1}}).filter(e=>e.index>=0).sort((e,t)=>t.index-e.index);return p([...n.map(e=>R.removeOp(G.record(t,e),i.records[e])),...a.map(({recordId:e,index:n})=>R.removeOp([`tables`,t,`recordOrder`,n],e)),...this._deleteAttachmentResources(i,t,e=>{let t=e.slice(e.lastIndexOf(``)+1);return r.has(t)})])}duplicateRecord(e,t,n,r){return this.createRecord(e,t,{...r,values:{...e.tables[t].records[n].values,...r.values,[j]:r.id}})}updateRecordOrder(e,t,n,r){let i=e.tables[t],a=[...G.record(t,n),`orderKey`],o=Object.values(i.records).map(e=>e.id===n?{...e,orderKey:r}:e).sort((e,t)=>String(e.orderKey??e.id).localeCompare(String(t.orderKey??t.id))).map(e=>e.id),s=[...G.table(t),`recordOrder`],c=i.recordOrder,l=o.filter(e=>e!==n),u=c==null?void 0:c.filter(e=>e!==n),d=c!=null&&c.includes(n)&&(u==null?void 0:u.length)===l.length&&u.every((e,t)=>e===l[t])?jt(s,c,o,n):this._replaceOptional(s,c,o);return p([this._replaceOptional(a,i.records[n].orderKey,r),d])}updateCell(e,t,n,r,i){var a,o;let s=e.tables[t],c=Lt(s,n,r)?s:m(s),l=(a=c.rowIndex)==null?void 0:a[n],u=(o=c.colIndex)==null?void 0:o[r];if(l==null||u==null){var d;return this._replaceOptional(G.cell(t,n,r),(d=e.tables[t])==null||(d=d.records[n])==null?void 0:d.values[r],i)}return this.setRangeValues(e,t,[{row:l,col:u,value:i}])}setRecordValues(e,t,n){let r=e.tables[t],i=n.every(e=>Object.keys(e.values).every(t=>Lt(r,e.recordId,t)))?r:m(r),a=n.flatMap(e=>Object.entries(e.values).flatMap(([t,n])=>{var r,a;let o=(r=i.rowIndex)==null?void 0:r[e.recordId],s=(a=i.colIndex)==null?void 0:a[t];return o==null||s==null?[]:[{row:o,col:s,value:n}]})),o=n.flatMap(n=>Object.entries(n.recordPatch??{}).map(([r,i])=>{var a;let o=[...G.record(t,n.recordId),r],s=(a=e.tables[t])==null||(a=a.records[n.recordId])==null?void 0:a[r];return this._replaceOptional(o,s,i)}));return p([this.setRangeValues(e,t,a),...o])}setRangeValues(e,t,r){let i=e.tables[t],a=r.every(e=>Rt(i,e.row,e.col))?i:m(i),o=/* @__PURE__ */ new Map,s=r.flatMap(e=>{var r,i,s,c;let l=(r=a.colId)==null?void 0:r[e.col],u=(i=a.rowId)==null?void 0:i[e.row],d=l?a.fields[l]:void 0;if(l===j||(d==null?void 0:d.type)===M.RecordId)throw Error(`[BaseJson1OpFactory]: cannot write record-id.`);let f=n(e.value,d),p=(s=a.cellData)==null||(s=s[e.row])==null?void 0:s[e.col],m=this._replaceOptional(G.cellData(t,e.row,e.col),p,f);if(!l||!u)return[m];let h=(c=a.records[u])==null?void 0:c.values[l],_=(d==null?void 0:d.type)===M.Attachment?Pe(e.value):S(e.value,f,d,a,u),v=[m,this._replaceOptional(G.cell(t,u,l),h,_)];return(d==null?void 0:d.type)===M.Attachment&&o.set(g(l,u),{recordId:u,fieldId:l,attachments:_}),v});return s.length?p([...s,...this._setAttachmentResources(a,t,Array.from(o.values()))]):R.replaceOp([`tables`,t,`cellData`],a.cellData,a.cellData)}createField(e,t,n){let r=e.tables[t];if(r.fields[n.field.id]||r.fieldOrder.includes(n.field.id))return R.replaceOp([`tables`,t,`fieldOrder`],r.fieldOrder,r.fieldOrder);let i=Math.max(1,n.index??r.fieldOrder.length),a=Object.values(r.views??{}).flatMap(e=>{var r;if(!e.fieldOrder||e.fieldOrder.includes(n.field.id))return[];let a=Ht(((r=n.viewIndexes)==null?void 0:r[e.id])??i,e.fieldOrder.length);return[R.insertOp([...G.view(t,e.id),`fieldOrder`,a],n.field.id)]});return p([...n.field.type===M.Formula?this._materializeFormulaTableNames(e):[],R.insertOp(G.field(t,n.field.id),n.field),R.insertOp(G.fieldOrder(t,i),n.field.id),...a])}duplicateField(e,t,n,r){let i=e.tables[t];if(!i.fields[n]||i.fields[r.field.id]||i.fieldOrder.includes(r.field.id))return R.replaceOp([`tables`,t,`fieldOrder`],i.fieldOrder,i.fieldOrder);let a=r.index??i.fieldOrder.indexOf(n)+1,o=i.fieldOrder.filter(e=>e!==r.field.id),s=Bt(o,r.field.id,a),c=Object.values(i.views??{}).flatMap(e=>{var i,s,c,l;let u=(i=e.fieldOrder)!=null&&i.length?e.fieldOrder:o;if(u.includes(r.field.id))return[];let d=Bt(u,r.field.id,((s=r.viewIndexes)==null?void 0:s[e.id])??a),f=(c=e.fieldSettings)==null?void 0:c[n];return[this._replaceOptional([...G.view(t,e.id),`fieldOrder`],e.fieldOrder,d),...f?[this._replaceOptional([...G.view(t,e.id),`fieldSettings`,r.field.id],(l=e.fieldSettings)==null?void 0:l[r.field.id],f)]:[]]}),l=Object.values(i.records).filter(e=>Object.prototype.hasOwnProperty.call(e.values,n)).map(e=>this._replaceOptional(G.cell(t,e.id,r.field.id),e.values[r.field.id],e.values[n]));return p([...r.field.type===M.Formula?this._materializeFormulaTableNames(e):[],R.insertOp(G.field(t,r.field.id),r.field),R.replaceOp([`tables`,t,`fieldOrder`],i.fieldOrder,s),...c,...l])}updateField(e,t,n,r){let i=e.tables[t].fields[n],a={...i,...r};Object.keys(r).forEach(e=>{r[e]===void 0&&delete a[e]});let o=Mt(e.tables[t],n,i,a);a=o.field;let s=G.field(t,n),c=i.type===a.type?this._patchProperties(s,i,{...r,...a.config===i.config?{}:{config:a.config},...a.defaultValue===i.defaultValue?{}:{defaultValue:a.defaultValue}}):R.replaceOp(s,i,a);return a.type===M.Formula?p([...this._materializeFormulaTableNames(e),c,...this._setFormulaFieldCells(e,t,n,a)]):p([c,...o.patches.length?[this.setRangeValues(e,t,o.patches)]:[]])}changeFieldType(e,t,n,r){return this.updateField(e,t,n,r)}deleteField(e,t,n){let r=e.tables[t];if(!(r!=null&&r.fields[n])||r.primaryFieldId===n||n===j)return R.replaceOp([`tables`,t,`fieldOrder`],r==null?void 0:r.fieldOrder,r==null?void 0:r.fieldOrder);let i=r.fieldOrder.indexOf(n),a=Object.values(r.views).flatMap(e=>{var r,i,a,o,s,c,l;let u=[],d=((r=e.fieldOrder)==null?void 0:r.indexOf(n))??-1;d>=0&&u.push(R.removeOp([...G.view(t,e.id),`fieldOrder`,d],n));let f=(i=e.fieldSettings)==null?void 0:i[n];f!==void 0&&u.push(R.removeOp([...G.view(t,e.id),`fieldSettings`,n],f));let p=e.filter?{...e.filter,conditions:e.filter.conditions.filter(e=>e.fieldId!==n)}:e.filter;p&&p.conditions.length!==((a=e.filter)==null?void 0:a.conditions.length)&&u.push(this._replaceOptional([...G.view(t,e.id),`filter`],e.filter,p));let m=(o=e.sort)==null?void 0:o.filter(e=>e.fieldId!==n);m&&m.length!==((s=e.sort)==null?void 0:s.length)&&u.push(this._replaceOptional([...G.view(t,e.id),`sort`],e.sort,m));let h=(c=e.group)==null?void 0:c.filter(e=>e.fieldId!==n);h&&h.length!==((l=e.group)==null?void 0:l.length)&&u.push(this._replaceOptional([...G.view(t,e.id),`group`],e.group,h));let g=Vt(e.config,n);return JSON.stringify(g)!==JSON.stringify(e.config)&&u.push(R.replaceOp([...G.view(t,e.id),`config`],e.config,g)),u}),o=Object.values(r.records).flatMap(e=>Object.prototype.hasOwnProperty.call(e.values,n)?[R.removeOp(G.cell(t,e.id,n),e.values[n])]:[]);return p([R.removeOp(G.field(t,n),r.fields[n]),R.removeOp(G.fieldOrder(t,i),n),...a,...o,...this._deleteAttachmentResources(r,t,e=>e.startsWith(`${n}`))])}moveField(e,t,n,r){let i=e.tables[t].fieldOrder;if(n===j||r.beforeFieldId===j)throw Error(`[BaseJson1OpFactory]: cannot move a field before record-id.`);let a=zt(i,n,r);return jt([`tables`,t,`fieldOrder`],i,a,n)}createView(e,t,n,r=e.tables[t].viewOrder.length){return p([R.insertOp(G.view(t,n.id),n),R.insertOp([`tables`,t,`viewOrder`,r],n.id)])}renameView(e,t,n,r){return R.replaceOp([...G.view(t,n),`name`],e.tables[t].views[n].name,r)}deleteView(e,t,n){let r=e.tables[t],i=r.views[n],a=r.viewOrder.indexOf(n);return p([R.removeOp(G.view(t,n),i),R.removeOp([`tables`,t,`viewOrder`,a],n)])}moveView(e,t,n,r){let i=e.tables[t].viewOrder,a=zt(i,n,{beforeFieldId:r.beforeViewId,afterFieldId:r.afterViewId});return jt([`tables`,t,`viewOrder`],i,a,n)}updateViewConfig(e,t,n,r){let i=e.tables[t].views[n].config;return this._patchProperties([...G.view(t,n),`config`],i,r)}setViewFilter(e,t,n,r){return this._replaceOptional([...G.view(t,n),`filter`],e.tables[t].views[n].filter,r)}setViewSort(e,t,n,r){return this._replaceOptional([...G.view(t,n),`sort`],e.tables[t].views[n].sort,r)}setViewGroup(e,t,n,r){return this._replaceOptional([...G.view(t,n),`group`],e.tables[t].views[n].group,r)}setViewFieldVisible(e,t,n,r,i){return this._setViewFieldSetting(e,t,n,r,{hidden:!i})}setViewFieldWidth(e,t,n,r,i){return this._setViewFieldSetting(e,t,n,r,{width:i})}getViewFieldSettingHistory(e,t,n,r,i){var a;let o=(a=e.tables[t].views[n].fieldSettings)==null?void 0:a[r],s=[...G.view(t,n),`fieldSettings`,r],c=this._patchProperties(s,o??{},{...i});return{redo:c,undo:R.type.invert(c)}}setViewFieldOrder(e,t,n,r,i){let a=e.tables[t].views[n],o=a.fieldOrder??e.tables[t].fieldOrder,s=zt(o,r,i),c=[...G.view(t,n),`fieldOrder`];return a.fieldOrder?jt(c,o,s,r):R.insertOp(c,s)}_setViewFieldSetting(e,t,n,r,i){let a=e.tables[t].views[n].fieldSettings,o=[...G.view(t,n),`fieldSettings`];if(!a)return this._replaceOptional(o,a,{[r]:i});let s=a[r];return s?p(Object.entries(i).map(([e,t])=>this._replaceOptional([...o,r,e],s[e],t))):this._replaceOptional([...o,r],s,i)}_createRecordOrderOp(e,t,n){let r=e.tables[t],i=new Set(n.map(e=>e.id)),a=[...Object.values(r.records).filter(e=>!i.has(e.id)),...n].sort((e,t)=>String(e.orderKey??e.id).localeCompare(String(t.orderKey??t.id))).map(e=>e.id),o=[...G.table(t),`recordOrder`],s=a.filter(e=>!i.has(e));if(!r.recordOrder||r.recordOrder.length!==s.length||s.some((e,t)=>e!==r.recordOrder[t]))return this._replaceOptional(o,r.recordOrder,a);let c=new R.WriteCursor;return c.descendPath(o),a.forEach((e,t)=>{i.has(e)&&(c.descend(t),c.write(`i`,e),c.ascend())}),c.get()}_materializeFormulaTableNames(e,t=/* @__PURE__ */ new Set){let n=ot(e);return Object.values(e.tables).filter(e=>!t.has(e.id)).sort((e,t)=>e.id.localeCompare(t.id)).flatMap(e=>{let t=n.get(e.id);return!t||e.formulaName===t?[]:[this._replaceOptional([...G.table(e.id),`formulaName`],e.formulaName,t)]})}_setFormulaFieldCells(e,t,n,r){var i,a;let o=m(e.tables[t]),s=String(((i=r.config)==null?void 0:i.formula)??``).trim(),c=(a=o.colIndex)==null?void 0:a[n];if(c==null)return[];let l=o.recordOrder??Object.keys(o.records),u=l.map(e=>{var t;return(t=o.rowIndex)==null?void 0:t[e]}).find(e=>e!=null);return l.flatMap(e=>{var r,i;let a=(r=o.rowIndex)==null?void 0:r[e];if(a==null)return[];let l=(i=o.cellData)==null||(i=i[a])==null?void 0:i[c],d=s?a===u?{f:s,si:n}:{si:n}:{v:null,t:null};return[this._replaceOptional(G.cellData(t,a,c),l,d)]})}_setAttachmentResources(e,t,n){var r;let i=new Map(Object.entries(((r=e.resources)==null?void 0:r.attachmentSets)??{})),a=/* @__PURE__ */ new Map,o=/* @__PURE__ */ new Set,s=n.map(({recordId:n,fieldId:r,attachments:s})=>{var c;let l=g(r,n),u=s.map((e,t)=>String(e.id??`${l}\u001F${t}`));return o.add(l),i.set(l,u),s.forEach((e,t)=>{let n=u[t];a.set(n,{...e,id:n})}),this._replaceOptional(G.attachmentSet(t,l),(c=e.resources)==null||(c=c.attachmentSets)==null?void 0:c[l],u)}),c=Array.from(a).map(([n,r])=>{var i;return this._replaceOptional(G.attachment(t,n),(i=e.resources)==null||(i=i.attachments)==null?void 0:i[n],r)}),l=new Set(Array.from(i.values()).flat()),u=new Set(Array.from(o).flatMap(t=>{var n;return((n=e.resources)==null||(n=n.attachmentSets)==null?void 0:n[t])??[]})),d=Array.from(u).flatMap(n=>{var r;let i=(r=e.resources)==null||(r=r.attachments)==null?void 0:r[n];return i!==void 0&&!l.has(n)?[R.removeOp(G.attachment(t,n),i)]:[]});return[...s,...c,...d]}_deleteAttachmentResources(e,t,n){var r;let i=((r=e.resources)==null?void 0:r.attachmentSets)??{},a=Object.entries(i).filter(([e])=>n(e));if(!a.length)return[];let o=new Set(Object.entries(i).filter(([e])=>!n(e)).flatMap(([,e])=>e)),s=new Set(a.flatMap(([,e])=>e));return[...a.map(([e,n])=>R.removeOp(G.attachmentSet(t,e),n)),...Array.from(s).flatMap(n=>{var r;let i=(r=e.resources)==null||(r=r.attachments)==null?void 0:r[n];return i!==void 0&&!o.has(n)?[R.removeOp(G.attachment(t,n),i)]:[]})]}_replaceOptional(e,t,n){return t===void 0?R.insertOp(e,n):R.replaceOp(e,t,n)}_patchProperties(e,t,n){let r=Object.entries(n);return t?p(r.map(([n,r])=>{let i=t[n];return r===i?null:r===void 0?R.removeOp([...e,n],i):this._replaceOptional([...e,n],i,r)})):R.insertOp(e,Object.fromEntries(r.filter(([,e])=>e!==void 0)))}};function jt(e,t,n,r){let i=t.indexOf(r),a=n.indexOf(r);return i===a?null:R.moveOp([...e,i],[...e,a])}function Mt(e,t,n,r){var i;let a=r.type;if(n.type!==a||a!==M.SingleSelect&&a!==M.MultiSelect||Nt(r.config.optionSource))return{field:r,patches:[]};let o=Pt(n),s=Pt(r);if(![...o].some(e=>!s.has(e)))return{field:r,patches:[]};let c=Ft(r.defaultValue,s,a),l=It(c,r.defaultValue)?r:{...r,defaultValue:c},u=m(e),d=(i=u.colIndex)==null?void 0:i[t];return d==null?{field:l,patches:[]}:{field:l,patches:(u.recordOrder??[]).flatMap(e=>{var n,r;let i=(n=u.rowIndex)==null?void 0:n[e],o=(r=u.records[e])==null?void 0:r.values[t],c=Ft(o,s,a);return i==null||It(c,o)?[]:[{row:i,col:d,value:c??null}]})}}function Nt(e){return typeof e==`object`&&!!e&&`type`in e&&e.type===`field`}function Pt(e){var t;let n=Array.isArray((t=e.config)==null?void 0:t.options)?e.config.options:[];return new Set(n.flatMap(e=>{if(!e||typeof e!=`object`||!(`id`in e))return[];let t=String(e.id);return t?[t]:[]}))}function Ft(e,t,n){if(e==null||e===``)return e;if(n===M.SingleSelect)return t.has(String(e))?e:null;let r=Array.isArray(e)?e.map(String):String(e).split(`,`).map(e=>e.trim()).filter(Boolean),i=r.filter(e=>t.has(e));return i.length===r.length?e:Array.isArray(e)?i:String(e).includes(`,`)?i.join(`,`):i[0]??[]}function It(e,t){return JSON.stringify(e)===JSON.stringify(t)}function Lt(e,t,n){var r,i,a,o;let s=(r=e.rowIndex)==null?void 0:r[t],c=(i=e.colIndex)==null?void 0:i[n];return s!=null&&c!=null&&((a=e.rowId)==null?void 0:a[s])===t&&((o=e.colId)==null?void 0:o[c])===n&&e.cellData!=null}function Rt(e,t,n){var r,i;return((r=e.rowId)==null?void 0:r[t])!=null&&((i=e.colId)==null?void 0:i[n])!=null&&e.cellData!=null}function zt(e,t,n){let r=e.filter(e=>e!==t),i=n.beforeFieldId?r.indexOf(n.beforeFieldId):n.afterFieldId?r.indexOf(n.afterFieldId)+1:r.length;return r.splice(i<0?r.length:i,0,t),r}function Bt(e,t,n){let r=e.filter(e=>e!==t);return r.splice(Ht(n,r.length),0,t),r}function Vt(e,t,n){if(Array.isArray(e))return n===`fieldIds`?e.filter(e=>e!==t):e.flatMap(e=>{if(e&&typeof e==`object`&&!Array.isArray(e)&&e.fieldId===t)return[];let n=Vt(e,t);return n===void 0?[]:[n]});if(!e||typeof e!=`object`)return n!=null&&n.endsWith(`FieldId`)&&e===t?void 0:e;let r=e;if(r.fieldId!==t)return Object.fromEntries(Object.entries(r).flatMap(([e,r])=>{if(e===t&&n===`fieldSettings`)return[];let i=Vt(r,t,e);return i===void 0?[]:[[e,i]]}))}function Ht(e,t){return Math.max(0,Math.min(e,t))}var Ut=class{constructor(e){this.unitId=e,D(this,`type`,W.Base),D(this,`subType`,U.Comment),D(this,`status`,z.INIT),D(this,`id`,void 0),D(this,`value`,!0),this.id=`${this.type}.${this.subType}_${e}`}},Wt=class{constructor(e){this.unitId=e,D(this,`type`,W.Base),D(this,`subType`,U.Copy),D(this,`status`,z.INIT),D(this,`id`,void 0),D(this,`value`,!0),this.id=`${this.type}.${this.subType}_${e}`}},Gt=class{constructor(e){this.unitId=e,D(this,`type`,W.Base),D(this,`subType`,U.Edit),D(this,`status`,z.INIT),D(this,`id`,void 0),D(this,`value`,!0),this.id=`${this.type}.${this.subType}_${e}`}},Kt=class{constructor(e){this.unitId=e,D(this,`type`,W.Base),D(this,`subType`,U.Export),D(this,`status`,z.INIT),D(this,`id`,void 0),D(this,`value`,!0),this.id=`${this.type}.${this.subType}_${e}`}},qt=class{constructor(e,t){this.unitId=e,this.objectId=t,D(this,`type`,W.BaseDashboard),D(this,`subType`,U.Edit),D(this,`status`,z.INIT),D(this,`id`,void 0),D(this,`value`,!0),this.id=`${this.type}.${this.subType}_${e}_${t}`}},Jt=class{constructor(e,t){this.unitId=e,this.objectId=t,D(this,`type`,W.BaseField),D(this,`subType`,U.Edit),D(this,`status`,z.INIT),D(this,`id`,void 0),D(this,`value`,!0),this.id=`${this.type}.${this.subType}_${e}_${t}`}},Yt=class{constructor(e,t){this.unitId=e,this.objectId=t,D(this,`type`,W.BaseRecord),D(this,`subType`,U.Edit),D(this,`status`,z.INIT),D(this,`id`,void 0),D(this,`value`,!0),this.id=`${this.type}.${this.subType}_${e}_${t}`}},Xt=class{constructor(e,t){this.unitId=e,this.objectId=t,D(this,`type`,W.BaseTable),D(this,`subType`,U.Edit),D(this,`status`,z.INIT),D(this,`id`,void 0),D(this,`value`,!0),this.id=`${this.type}.${this.subType}_${e}_${t}`}},Zt=class{constructor(e,t){this.unitId=e,this.objectId=t,D(this,`type`,W.BaseView),D(this,`subType`,U.Edit),D(this,`status`,z.INIT),D(this,`id`,void 0),D(this,`value`,!0),this.id=`${this.type}.${this.subType}_${e}_${t}`}};const Qt=[U.Edit,U.Copy,U.Export,U.Comment],$t=/* @__PURE__ */ new Set([W.Base,W.BaseTable,W.BaseField,W.BaseRecord,W.BaseView,W.BaseDashboard]),en=`table/`,tn=`field/`,nn=`record/`,rn=`view/`,an=`dashboard/`;function q(e){return`${en}${encodeURIComponent(e)}`}function J(e,t){return`${tn}${encodeURIComponent(e)}/${encodeURIComponent(t)}`}function Y(e,t){return`${nn}${encodeURIComponent(e)}/${encodeURIComponent(t)}`}function on(e,t){return`${rn}${encodeURIComponent(e)}/${encodeURIComponent(t)}`}function sn(e){return`${an}${encodeURIComponent(e)}`}function cn(e,t,n){if(t===e)switch(n){case U.Edit:return new Gt(e);case U.Copy:return new Wt(e);case U.Export:return new Kt(e);case U.Comment:return new Ut(e);default:throw Error(`Unsupported Base permission action: ${n}`)}if(n!==U.Edit)throw Error(`Base object permissions only support Edit: ${t}`);if(t.startsWith(en))return new Xt(e,t);if(t.startsWith(tn))return new Jt(e,t);if(t.startsWith(nn))return new Yt(e,t);if(t.startsWith(rn))return new Zt(e,t);if(t.startsWith(an))return new qt(e,t);throw Error(`Unsupported Base permission object: ${t}`)}function ln(e,t,n,r){var i;return((i=e.getPermissionPoint(cn(t,n,r).id))==null?void 0:i.value)??!0}function un(e,t,n,r,i){let a=cn(t,n,r);e.getPermissionPoint(a.id)||e.addPermissionPoint(a),e.updatePermissionPoint(a.id,i)}function dn(e,t,n,r){e.deletePermissionPoint(cn(t,n,r).id)}function fn(e,t){e.getAllPermissionPoint().forEach((n,r)=>{n.subscribe(n=>{$t.has(n.type)&&`unitId`in n&&n.unitId===t&&e.deletePermissionPoint(r)}).unsubscribe()})}function pn(e,t,n){return ln(e,t,t,U.Edit)&&Array.from(n).every(n=>ln(e,t,n,U.Edit))}const mn=ct(`base.permission.service`);let hn=class extends Ge{constructor(e,t){super(),this._permissionService=e,this._instanceService=t,D(this,`_basePermissions`,/* @__PURE__ */ new Map),D(this,`_tablePermissions`,/* @__PURE__ */ new Map),D(this,`_fieldPermissions`,/* @__PURE__ */ new Map),D(this,`_recordPermissions`,/* @__PURE__ */ new Map),D(this,`_viewPermissions`,/* @__PURE__ */ new Map),D(this,`_tableBaseIds`,/* @__PURE__ */ new Map),this.disposeWithMe(this._instanceService.getTypeOfUnitDisposed$(V.UNIVER_BASE).subscribe(e=>this._clearUnit(e.getUnitId())))}canCreateTable(e){var t;return this._canEdit(e,[])&&(((t=this._basePermissions.get(e))==null?void 0:t.createTable)??!0)}canEditTable(e){return this._canEditTable(e)}canDeleteTable(e){var t;return this._canEditTable(e)&&(((t=this._tablePermissions.get(e))==null?void 0:t.delete)??!0)}canCreateField(e){var t;return this._canEditTable(e)&&(((t=this._tablePermissions.get(e))==null?void 0:t.createField)??!0)}canEditField(e,t){var n;return this._canEditTable(e)&&this._canEdit(e,[J(e,t)])&&(((n=this._fieldPermissions.get(_n(e,t)))==null?void 0:n.edit)??!0)}canDeleteField(e,t){var n;return this.canEditField(e,t)&&(((n=this._fieldPermissions.get(_n(e,t)))==null?void 0:n.delete)??!0)}canCreateRecord(e){var t;return this._canEditTable(e)&&(((t=this._tablePermissions.get(e))==null?void 0:t.createRecord)??!0)}canEditRecord(e,t){var n;let r=this._recordPermissions.get(vn(e,t));return this._canEdit(e,[q(e),Y(e,t)])&&(((n=this._tablePermissions.get(e))==null?void 0:n.edit)??!0)&&((r==null?void 0:r.edit)??!0)&&((r==null?void 0:r.editCell)??!0)}canDeleteRecord(e,t){var n,r,i;return this._canEdit(e,[q(e),Y(e,t)])&&(((n=this._tablePermissions.get(e))==null?void 0:n.edit)??!0)&&(((r=this._recordPermissions.get(vn(e,t)))==null?void 0:r.edit)??!0)&&(((i=this._recordPermissions.get(vn(e,t)))==null?void 0:i.delete)??!0)}canEditCell(e,t,n){var r,i,a;let o=this._fieldPermissions.get(_n(e,n));return this._canEdit(e,[q(e),J(e,n),Y(e,t)])&&(((r=this._tablePermissions.get(e))==null?void 0:r.edit)??!0)&&(((i=this._recordPermissions.get(vn(e,t)))==null?void 0:i.edit)??!0)&&(((a=this._recordPermissions.get(vn(e,t)))==null?void 0:a.editCell)??!0)&&((o==null?void 0:o.edit)??!0)&&((o==null?void 0:o.editCell)??!0)}canManageView(e,t){var n,r,i;return this._canEdit(e,[q(e),on(e,t)])&&(((n=this._tablePermissions.get(e))==null?void 0:n.edit)??!0)&&(((r=this._viewPermissions.get(yn(e,t)))==null?void 0:r.edit)??!0)&&(((i=this._tablePermissions.get(e))==null?void 0:i.manageView)??!0)}setBasePermission(e,t){this._basePermissions.set(e,{...this._basePermissions.get(e),...t}),t.edit!==void 0&&un(this._permissionService,e,e,U.Edit,t.edit)}setTablePermission(e,t){this._rememberTable(e),this._tablePermissions.set(e,{...this._tablePermissions.get(e),...t}),this._setObjectEdit(e,q(e),t.edit)}setFieldPermission(e,t,n){this._rememberTable(e);let r=_n(e,t);this._fieldPermissions.set(r,{...this._fieldPermissions.get(r),...n}),this._setObjectEdit(e,J(e,t),n.edit)}setRecordPermission(e,t,n){this._rememberTable(e);let r=vn(e,t);this._recordPermissions.set(r,{...this._recordPermissions.get(r),...n}),this._setObjectEdit(e,Y(e,t),n.edit)}setViewPermission(e,t,n){this._rememberTable(e);let r=yn(e,t);this._viewPermissions.set(r,{...this._viewPermissions.get(r),...n}),this._setObjectEdit(e,on(e,t),n.edit)}clearBasePermission(e){this._basePermissions.delete(e),dn(this._permissionService,e,e,U.Edit)}clearTablePermission(e){this._tablePermissions.delete(e),this._clearObjectEdit(e,q(e))}clearFieldPermission(e,t){this._fieldPermissions.delete(_n(e,t)),this._clearObjectEdit(e,J(e,t))}clearRecordPermission(e,t){this._recordPermissions.delete(vn(e,t)),this._clearObjectEdit(e,Y(e,t))}clearViewPermission(e,t){this._viewPermissions.delete(yn(e,t)),this._clearObjectEdit(e,on(e,t))}getSnapshot(){return{bases:Object.fromEntries(this._basePermissions),tables:Object.fromEntries(this._tablePermissions),fields:Object.fromEntries(this._fieldPermissions),records:Object.fromEntries(this._recordPermissions),views:Object.fromEntries(this._viewPermissions)}}_canEditTable(e){var t;return this._canEdit(e,[q(e)])&&(((t=this._tablePermissions.get(e))==null?void 0:t.edit)??!0)}_canEdit(e,t){var n;let r=this._resolveBaseId(e);return!r||pn(this._permissionService,r,t)&&(((n=this._basePermissions.get(r))==null?void 0:n.edit)??!0)}_setObjectEdit(e,t,n){if(n===void 0)return;let r=this._resolveBaseId(e);r&&un(this._permissionService,r,t,U.Edit,n)}_clearObjectEdit(e,t){let n=this._resolveBaseId(e);n&&dn(this._permissionService,n,t,U.Edit)}_resolveBaseId(e){var t;let n=this._instanceService.getUnit(e,V.UNIVER_BASE);return n?n.getUnitId():(t=this._instanceService.getAllUnitsForType(V.UNIVER_BASE).find(t=>!!t.getSnapshot().tables[e]))==null?void 0:t.getUnitId()}_rememberTable(e){let t=this._resolveBaseId(e);t&&this._tableBaseIds.set(e,t)}_clearUnit(e){this._basePermissions.delete(e),this._tableBaseIds.forEach((t,n)=>{t===e&&(this._tablePermissions.delete(n),gn(this._fieldPermissions,`${n}\u001F`),gn(this._recordPermissions,`${n}\u001F`),gn(this._viewPermissions,`${n}\u001F`),this._tableBaseIds.delete(n))})}};hn=O([E(0,Je),E(1,L)],hn);function gn(e,t){e.forEach((n,r)=>{r.startsWith(t)&&e.delete(r)})}function _n(e,t){return`${e}\u001F${t}`}function vn(e,t){return`${e}\u001F${t}`}function yn(e,t){return`${e}\u001F${t}`}const X=ct(`base.command.service`);let bn=class{constructor(e,t=xn(),n={}){this._permissionService=e,this._fieldRegistry=t,this._runtimeContext=n,D(this,`_factory`,new K),D(this,`_applier`,new k)}createTable(e,t,n){if(!this._permissionService.canCreateTable(e.getUnitId()))throw Error(`[BaseCommandService]: cannot create table.`);at(t),this._apply(e,this._factory.createTable(e.getSnapshot(),t,n))}renameTable(e,t,n){if(!this._permissionService.canEditTable(t))throw Error(`[BaseCommandService]: cannot edit table.`);this._apply(e,this._factory.renameTable(e.getSnapshot(),t,n))}deleteTable(e,t){if(!this._permissionService.canDeleteTable(t))throw Error(`[BaseCommandService]: cannot delete table.`);let n=ie(e.getSnapshot(),t);if(n.length>0)throw Error(`[BaseCommandService]: cannot delete a RecordLink target table (${n.map(e=>`${e.tableId}.${e.fieldId}`).join(`, `)}).`);this._apply(e,this._factory.deleteTable(e.getSnapshot(),t))}updateCell(e,t,n,r,i){this._apply(e,this.createUpdateCellOp(e,t,n,r,i))}setRecordValues(e,t,n){this._apply(e,this.createSetRecordValuesOp(e,t,n))}createRecord(e,t,n){this._apply(e,this.createCreateRecordOp(e,t,n))}createUpdateCellOp(e,t,n,r,i){let a=e.getSnapshot(),o=this._normalizeCellValue(a,t,n,r,i),s=this._withModifiedSystemValues(a,t,n,{[r]:o});return this._factory.setRecordValues(a,t,[s])}createSetRecordValuesOp(e,t,n){let r=e.getSnapshot(),i=n.map(e=>this._withModifiedSystemValues(r,t,e.recordId,{...Object.fromEntries(Object.entries(e.values).map(([n,i])=>[n,this._normalizeCellValue(r,t,e.recordId,n,i)]))}));return this._factory.setRecordValues(r,t,i)}createCreateRecordOp(e,t,n){if(!this._permissionService.canCreateRecord(t))throw Error(`[BaseCommandService]: cannot create record.`);let r=e.getSnapshot(),i=this._createNumberingAllocators(r.tables[t]);return this._factory.createRecord(r,t,this._createRecordWithDefaults(r,t,n,i))}createBatchCreateRecordsOp(e,t,n){if(!this._permissionService.canCreateRecord(t))throw Error(`[BaseCommandService]: cannot create record.`);let r=e.getSnapshot();return c(`BaseCommand.createBatchCreateRecordsOp`,()=>{let e=this._createNumberingAllocators(r.tables[t]);return this._factory.createRecords(r,t,n.map(n=>this._createRecordWithDefaults(r,t,n,e)))},{existingRecords:Object.keys(r.tables[t].records).length,records:n.length})}createCreateFieldOp(e,t,n,r,i){if(!this._permissionService.canCreateField(t))throw Error(`[BaseCommandService]: cannot create field.`);let a=e.getSnapshot();if(this._assertFieldDefinition(a,n),r===0)throw Error(`[BaseCommandService]: cannot insert a field before record-id.`);let o=this._factory.createField(a,t,{field:n,index:r,viewIndexes:i}),s=this._createSystemFieldValuePatches(a,t,n);if(!s.length)return o;let c=this._applier.applyOp(a,o);return p([o,this._factory.setRecordValues(c,t,s)])}deleteRecord(e,t,n){if(!this._permissionService.canDeleteRecord(t,n))throw Error(`[BaseCommandService]: cannot delete record.`);this._apply(e,this._factory.deleteRecord(e.getSnapshot(),t,n))}duplicateRecord(e,t,n,r){if(!this._permissionService.canCreateRecord(t))throw Error(`[BaseCommandService]: cannot create record.`);this._apply(e,this._factory.duplicateRecord(e.getSnapshot(),t,n,{...r,values:{...r.values,[j]:r.id}}))}updateRecordOrder(e,t,n,r){if(!this._permissionService.canEditRecord(t,n))throw Error(`[BaseCommandService]: cannot edit record.`);this._apply(e,this._factory.updateRecordOrder(e.getSnapshot(),t,n,r))}createField(e,t,n,r,i){this._apply(e,this.createCreateFieldOp(e,t,n,r,i))}updateField(e,t,n,r){var i;if(!this._permissionService.canEditField(t,n))throw Error(`[BaseCommandService]: cannot edit field.`);let a=e.getSnapshot(),o=(i=a.tables[t])==null?void 0:i.fields[n];if(!o||n===j)throw Error(`[BaseCommandService]: cannot edit record-id.`);this._assertFieldDefinition(a,{...o,...r}),this._apply(e,this._factory.updateField(a,t,n,r))}changeFieldType(e,t,n,r){var i;if(!this._permissionService.canEditField(t,n))throw Error(`[BaseCommandService]: cannot edit field.`);let a=e.getSnapshot(),o=(i=a.tables[t])==null?void 0:i.fields[n];if(!o||n===j)throw Error(`[BaseCommandService]: cannot change record-id field type.`);this._assertFieldDefinition(a,{...o,...r}),this._apply(e,this._factory.changeFieldType(a,t,n,r))}deleteField(e,t,n){var r;if(((r=e.getSnapshot().tables[t])==null?void 0:r.primaryFieldId)===n||n===j)throw Error(`[BaseCommandService]: cannot delete primary field.`);if(!this._permissionService.canDeleteField(t,n))throw Error(`[BaseCommandService]: cannot delete field.`);this._apply(e,this._factory.deleteField(e.getSnapshot(),t,n))}moveField(e,t,n,r){if(!this._permissionService.canEditField(t,n))throw Error(`[BaseCommandService]: cannot edit field.`);if(n===j||r.beforeFieldId===j)throw Error(`[BaseCommandService]: cannot move a field before record-id.`);this._apply(e,this._factory.moveField(e.getSnapshot(),t,n,r))}createView(e,t,n,r){if(!this._permissionService.canManageView(t,n.id))throw Error(`[BaseCommandService]: cannot manage view.`);this._apply(e,this._factory.createView(e.getSnapshot(),t,n,r))}renameView(e,t,n,r){if(!this._permissionService.canManageView(t,n))throw Error(`[BaseCommandService]: cannot manage view.`);this._apply(e,this._factory.renameView(e.getSnapshot(),t,n,r))}deleteView(e,t,n){if(!this._permissionService.canManageView(t,n))throw Error(`[BaseCommandService]: cannot manage view.`);this._apply(e,this._factory.deleteView(e.getSnapshot(),t,n))}moveView(e,t,n,r){if(!this._permissionService.canManageView(t,n))throw Error(`[BaseCommandService]: cannot manage view.`);this._apply(e,this._factory.moveView(e.getSnapshot(),t,n,r))}updateViewConfig(e,t,n,r){if(!this._permissionService.canManageView(t,n))throw Error(`[BaseCommandService]: cannot manage view.`);this._apply(e,this._factory.updateViewConfig(e.getSnapshot(),t,n,r))}setViewFilter(e,t,n,r){if(!this._permissionService.canManageView(t,n))throw Error(`[BaseCommandService]: cannot manage view.`);this._apply(e,this._factory.setViewFilter(e.getSnapshot(),t,n,r))}setViewSort(e,t,n,r){if(!this._permissionService.canManageView(t,n))throw Error(`[BaseCommandService]: cannot manage view.`);this._apply(e,this._factory.setViewSort(e.getSnapshot(),t,n,r))}setViewGroup(e,t,n,r){if(!this._permissionService.canManageView(t,n))throw Error(`[BaseCommandService]: cannot manage view.`);this._apply(e,this._factory.setViewGroup(e.getSnapshot(),t,n,r))}setViewFieldVisible(e,t,n,r,i){if(!this._permissionService.canManageView(t,n))throw Error(`[BaseCommandService]: cannot manage view.`);this._apply(e,this._factory.setViewFieldVisible(e.getSnapshot(),t,n,r,i))}setViewFieldWidth(e,t,n,r,i){if(!this._permissionService.canManageView(t,n))throw Error(`[BaseCommandService]: cannot manage view.`);this._apply(e,this._factory.setViewFieldWidth(e.getSnapshot(),t,n,r,i))}setViewFieldOrder(e,t,n,r,i){if(!this._permissionService.canManageView(t,n))throw Error(`[BaseCommandService]: cannot manage view.`);this._apply(e,this._factory.setViewFieldOrder(e.getSnapshot(),t,n,r,i))}_apply(e,t){let n=e.getSnapshot();e.setSnapshot(this._applier.applyOp(n,t))}_createRecordWithDefaults(e,t,n,r=this._createNumberingAllocators(e.tables[t])){let i=e.tables[t],o=n.createdBy??n.updatedBy??this._getCurrentUserId(),s={...n,createdBy:n.createdBy??o,updatedBy:n.updatedBy??o,values:{...n.values}};return s.values[j]=s.id,i.fieldOrder.forEach(e=>{let t=i.fields[e];if(t&&s.values[e]===void 0)switch(t.type){case M.Numbering:var n;s.values[e]=((n=r.get(e))==null?void 0:n())??this._createNumberingAllocator(i,e)();break;case M.CreatedBy:s.createdBy&&(s.values[e]=s.createdBy);break;case M.UpdatedBy:s.updatedBy&&(s.values[e]=s.updatedBy);break;case M.CreatedAt:s.values[e]=a(s.createdAt);break;case M.UpdatedAt:s.values[e]=a(s.updatedAt);break;default:{let n=t.defaultValue;n!=null&&n!==``&&!(Array.isArray(n)&&n.length===0)&&(s.values[e]=n)}}}),s}_withModifiedSystemValues(e,t,n,r){var i,o;let s=e.tables[t],c=((i=(o=this._runtimeContext).now)==null?void 0:i.call(o))??Date.now(),l=this._getCurrentUserId(),u={...r};return s.fieldOrder.forEach(e=>{let t=s.fields[e];t&&(t.type===M.UpdatedAt&&(u[e]=a(c)),t.type===M.UpdatedBy&&l&&(u[e]=l))}),{recordId:n,values:u,recordPatch:{updatedAt:c,...l?{updatedBy:l}:{}}}}_createSystemFieldValuePatches(e,t,n){let r=e.tables[t];return!r||!Sn(n)?[]:n.type===M.Numbering?this._createNumberingFieldValuePatches(r,n.id):Object.values(r.records).flatMap(e=>{let t=this._getSystemFieldValue(n,e);return t===void 0?[]:[{recordId:e.id,values:{[n.id]:t}}]})}_getSystemFieldValue(e,t){switch(e.type){case M.CreatedBy:return t.createdBy??this._getCurrentUserId();case M.UpdatedBy:return t.updatedBy??this._getCurrentUserId();case M.CreatedAt:return a(t.createdAt??Date.now());case M.UpdatedAt:return a(t.updatedAt??Date.now());default:return}}_createNumberingAllocators(e){let t=/* @__PURE__ */ new Map;return e&&e.fieldOrder.forEach(n=>{let r=e.fields[n];(r==null?void 0:r.type)===M.Numbering&&t.set(n,this._createNumberingAllocator(e,n))}),t}_createNumberingAllocator(e,t){let n=Object.values(e.records).reduce((e,n)=>{let r=Number(n.values[t]);return Number.isFinite(r)?Math.max(e,r):e},0);return()=>String(++n)}_createNumberingFieldValuePatches(e,t){let n=this._getOrderedRecords(e),r=this._createNumberingAllocator(e,t);return n.flatMap(e=>this._isNonEmptyCellValue(e.values[t])?[]:[{recordId:e.id,values:{[t]:r()}}])}_getOrderedRecords(e){var t;let n=Object.values(e.records),r=new Map(n.map(e=>[e.id,e])),i=[],a=/* @__PURE__ */ new Set;(t=e.recordOrder)==null||t.forEach(e=>{let t=r.get(e);t&&(i.push(t),a.add(e))});let o=n.filter(e=>!a.has(e.id)).sort((e,t)=>String(e.orderKey??e.id).localeCompare(String(t.orderKey??t.id))||e.id.localeCompare(t.id));return[...i,...o]}_isNonEmptyCellValue(e){return e!=null&&e!==``&&!(Array.isArray(e)&&e.length===0)}_getCurrentUserId(){var e,t,n,r;return((e=(t=this._runtimeContext).getCurrentUserId)==null?void 0:e.call(t))??((n=(r=this._runtimeContext).getCurrentUser)==null||(n=n.call(r))==null?void 0:n.userID)??void 0}_normalizeCellValue(e,t,n,r,i){var a;if(!this._permissionService.canEditCell(t,n,r))throw Error(`[BaseCommandService]: cannot edit cell.`);let o=(a=e.tables[t])==null?void 0:a.fields[r];if(!o)throw Error(`[BaseCommandService]: field "${r}" does not exist.`);let s=this._fieldRegistry.get(o.type);if(o.readonly||o.system||!s.getCapabilities(o).editable)throw Error(`[BaseCommandService]: cannot edit readonly field.`);let c=s.normalize(i,o),l=s.validate(c,o);if(!l.valid)throw Error(l.reason??`[BaseCommandService]: invalid cell value.`);let u=o.type===M.RecordLink?ge(e,o,c):{valid:!0};if(!u.valid)throw Error(u.reason??`[BaseCommandService]: invalid RecordLink value.`);return c}_assertFieldDefinition(e,t){if(t.id===j||t.type===M.RecordId||dt(t.name))throw Error(`[BaseCommandService]: record-id is reserved.`);let n=xe(e,t);if(!n.valid)throw Error(n.reason??`[BaseCommandService]: invalid field definition.`)}};bn=O([E(0,mn),E(1,Ne)],bn);function xn(){let e=new Ae;return Me().forEach(t=>e.register(t)),e}function Sn(e){return e.type===M.Numbering||e.type===M.CreatedBy||e.type===M.UpdatedBy||e.type===M.CreatedAt||e.type===M.UpdatedAt}const Cn=H.createRegExpFromSafeFragment(String.raw`(?:^|\W)\$?[A-Z]{1,3}\$?\d+(?::\$?[A-Z]{1,3}\$?\d+)?(?:$|\W)`),wn=H.createRegExpFromSafeFragment(String.raw`(?:'\[[^\]]+\](?:[^']|'')+'|\[[^\]]+\][^\s'!]+)!\$?[A-Z]{1,3}\$?\d+(?::\$?[A-Z]{1,3}\$?\d+)?`,`gi`),Tn=/\{([^}]+)\}/g,En=H.createRegExpFromSafeFragment(String.raw`\b([A-Z_][\w.]*)\[\[\s*#(This Row|Data)\s*\],\s*\[([^\]]+)\]\]`,`gi`),Dn=H.createRegExpFromSafeFragment(String.raw`\b([A-Z_][\w.]*)\[@\[([^\]]+)\]\]`,`gi`),On=H.createRegExpFromSafeFragment(String.raw`\b([A-Z_][\w.]*)\[([^\[\]#]+)\]`,`gi`),kn=H.createRegExpFromSafeFragment(String.raw`(^|[^\w\[])\[\[\s*#(This Row|Data)\s*\],\s*\[([^\]]+)\]\]`,`gi`),An=H.createRegExpFromSafeFragment(String.raw`(^|[^\w\[])\[@\[([^\]]+)\]\](?!\])`,`g`),jn=H.createRegExpFromSafeFragment(String.raw`(^|[^\w\[])\[@?([^\[\]#]+)\](?!\])`,`g`);var Mn=class{createSuperTableReference(e,t=e.id){var n;let r=((n=e.recordOrder)==null?void 0:n.length)??Object.values(e.records).length;return{sheetId:t,titleMap:new Map(e.fieldOrder.map((t,n)=>{var r;return[((r=e.fields[t])==null?void 0:r.name)??t,n]})),range:{startRow:0,endRow:Math.max(0,r-1),startColumn:0,endColumn:Math.max(0,e.fieldOrder.length-1)},showHeader:!1}}getSuperTableRegistrations(e,t){let n=[],r=t?new Set(t):null,i=ot(e);return Object.values(e.tables).filter(e=>!r||r.has(e.id)).forEach(e=>{let t=this.createSuperTableReference(e,e.id);n.push({tableId:e.id,tableName:i.get(e.id),reference:t})}),n}validateFormula(e,t,n={}){let r=e.trim();if(!r)return{valid:!0};if(Rn(r))return{valid:!1,reason:`[BaseFormula]: A1 references and ranges are not supported in Base formulas.`};let i=n.snapshot?ot(n.snapshot):void 0;for(let e of Ln(r)){let r=Kn(e,t,n.snapshot,i);if(!r.table)return{valid:!1,reason:`[BaseFormula]: table "${e.tableName}" does not exist. Use FBaseTable.getFormulaName() or getSchema().formulaName for structured references.`};if(!r.field)return{valid:!1,reason:`[BaseFormula]: field "${e.fieldName}" does not exist.`}}return{valid:!0}}getDependencies(e,t,n={}){let r=/* @__PURE__ */ new Set;for(let i of this.getStructuredDependencies(e,t,n))i.tableId===t.id&&r.add(i.fieldId);return Array.from(r)}getStructuredDependencies(e,t,n={}){let r=/* @__PURE__ */ new Map,i=n.snapshot?ot(n.snapshot):void 0;for(let a of Ln(e)){let e=Kn(a,t,n.snapshot,i);if(!e.table||!e.field)continue;let o=`${e.table.id}\u001F${e.field.id}`;r.set(o,{tableId:e.table.id,fieldId:e.field.id})}return Array.from(r.values())}};function Nn(e,t){var n;let r=[],i=(n=t.unitData)==null?void 0:n[e.id];return i&&Object.entries(i).forEach(([t,n])=>{let i=e.tables[t];if(!i||!n)return;let a=m(i),o=[];new $e(n).forValue((e,t,n)=>{let r=Pn(a,e,t,n);r&&o.push(r)}),o.length&&r.push({unitId:e.id,tableId:t,patches:o})}),r}function Pn(e,t,n,r){var i,a,s;let c=(i=e.rowId)==null?void 0:i[t],l=(a=e.colId)==null?void 0:a[n];if(!c||!e.records[c]||!l)return null;let u=e.fields[l];if(!u||u.type!==`formula`)return null;let d=String(((s=u.config)==null?void 0:s.formula)??``).trim(),f=(r==null?void 0:r.v)??null,p={v:f,t:In(f),...t===0&&d?{f:d}:{},si:u.id};return Fn(o(e,c,l),p)?null:{row:t,col:n,value:p}}function Fn(e,t){return e!=null&&Object.is(e.v??null,t.v??null)&&e.t===t.t&&e.f===t.f&&e.si===t.si}function In(e){return typeof e==`number`?Ve.NUMBER:typeof e==`boolean`?Ve.BOOLEAN:typeof e==`string`?Ve.STRING:null}function Ln(e){let t=Bn(e,()=>``),n=/* @__PURE__ */ new Map,r=e=>{n.set(`${e.tableName??``}\u001F${e.fieldName}`,e)};for(let e of t.matchAll(Tn))r({fieldName:e[1],scope:`this-row`});t=t.replace(En,(e,t,n,i)=>(r({tableName:t,fieldName:i,scope:n.toLowerCase()===`data`?`data`:`this-row`}),` `)),t=t.replace(Dn,(e,t,n)=>(r({tableName:t,fieldName:n,scope:`this-row`}),` `)),t=t.replace(kn,(e,t,n,i)=>(r({fieldName:i,scope:n.toLowerCase()===`data`?`data`:`this-row`}),`${t} `)),t=t.replace(An,(e,t,n)=>(r({fieldName:n,scope:`this-row`}),`${t} `));for(let e of t.matchAll(On))r({tableName:e[1],fieldName:e[2]});for(let e of t.matchAll(jn))r({fieldName:e[2],scope:`this-row`});return Array.from(n.values())}function Rn(e){let t=Bn(e,()=>` `).replace(Tn,` `).replace(En,` `).replace(Dn,` `).replace(kn,`$1 `).replace(An,`$1 `).replace(On,` `).replace(jn,`$1 `);return Cn.test(t)}function zn(e,t){return Wn(Hn(e,t),t)}function Bn(e,t){return Vn(zn(e,t),t)}function Vn(e,t){return e.replace(/"(?:""|[^"])*"/g,t)}function Hn(e,t){return wn.lastIndex=0,e.replace(wn,(n,r)=>Gn(e,r)?n:t(n))}const Un=/(?:'((?:[^']|'')+)'|\[[^\]]+\]|[A-Za-z0-9_.-]+)![^\s!\[\]]+\[/g;function Wn(e,t){let n=[];for(Un.lastIndex=0;;){let t=Un.exec(e);if(t==null)break;if(Gn(e,t.index))continue;let r=Un.lastIndex-1,i=0;for(let a=r;a<e.length;a++)if(e[a]===`[`&&i++,e[a]===`]`&&(i--,i===0)){n.push({start:t.index,end:a+1}),Un.lastIndex=a+1;break}}if(n.length===0)return e;let r=``,i=0;for(let a of n)r+=e.slice(i,a.start),r+=t(e.slice(a.start,a.end)),i=a.end;return r+e.slice(i)}function Gn(e,t){let n=!1;for(let r=0;r<t;r++)e[r]===`"`&&(n&&e[r+1]===`"`?r++:n=!n);return n}function Kn(e,t,n,r){let i=qn(e.tableName,t,n,r);return{table:i,field:i?Object.values(i.fields).find(t=>t.name===e.fieldName):void 0}}function qn(e,t,n,r){if(!e)return t;if(!n){var i;let n=e.toLowerCase();return n===t.id.toLowerCase()||n===t.name.toLowerCase()||n===((i=t.formulaName)==null?void 0:i.toLowerCase())?t:void 0}let a=e.toLowerCase(),o=r??ot(n),s=Object.values(n.tables).filter(e=>{var t;return e.id.toLowerCase()===a||e.name.toLowerCase()===a||((t=o.get(e.id))==null?void 0:t.toLowerCase())===a||Jn(e.id).toLowerCase()===a});return s.length===1?s[0]:void 0}function Jn(e){return`_T_${Array.from(e,e=>{var t;return/[A-Za-z0-9]/.test(e)?e:`_x${((t=e.codePointAt(0))==null?void 0:t.toString(16))??`0`}_`}).join(``)}`}const Yn=new Mn;function Xn(e,t,n){var r;if(n.type!==M.Formula)return!0;let i=String(((r=n.config)==null?void 0:r.formula)??``),a=e.getSnapshot();return Yn.validateFormula(i,a.tables[t],{snapshot:a}).valid}function Z(e,t,n){let r=e.getSnapshot().tables[t];if(!r)return!1;let{viewIds:i,fieldIds:a,recordIds:o}=n||{};if(i){for(let e of i)if(!r.views[e])return!1}if(a){for(let e of a)if(!r.fields[e])return!1}if(o){for(let e of o)if(!r.records[e])return!1}return!0}function Zn(e,t,n){if(n.id===j||n.type===M.RecordId||dt(n.name)||!Xn(e,t,n))return!1;let r=e.getSnapshot();if(!xe(r,n).valid)return!1;if(n.type!==M.RecordLink)return!0;let i=me(n);if(!i)return!1;if(i.relationRole===void 0)return!0;if(i.relationRole!==N.Parent||i.targetTableId!==t||i.multiple!==!1)return!1;let a=r.tables[t];return!Object.values(a.fields).some(e=>{var t;return e.id!==n.id&&e.type===M.RecordLink&&((t=me(e))==null?void 0:t.relationRole)===N.Parent})}function Qn(e,t,n,r){var i;let a=e.getSnapshot(),o=(i=a.tables[t])==null?void 0:i.fields[n];if(!o||o.id===j||o.type===M.RecordId)return!1;if(o.type!==M.RecordLink)return!0;let s=tr(r);return ge(a,o,s).valid}function $n(e,t,n){let r=e.getSnapshot().tables[t];return!r||!ft(n.id)||r.records[n.id]||Object.prototype.hasOwnProperty.call(n.values,j)&&n.values[j]!==n.id?!1:Object.entries(n.values).every(([n,r])=>n===j||Qn(e,t,n,r))}function er(e){return{...e,values:{...e.values,[j]:e.id}}}function tr(e){if(e&&typeof e==`object`&&!Array.isArray(e)&&`v`in e){let t=e.v;return t===void 0?null:t}return e}const nr={id:`base.command.set-base-record-values`,type:P.COMMAND,handler(e,t){if(!t)return!1;let n=e.get(L),{unitId:r,tableId:i,patches:a}=t,o=n.getUnit(r,V.UNIVER_BASE),s=a.map(e=>e.recordId),c=a.flatMap(e=>Object.keys(e.values));if(!o||!Z(o,i,{recordIds:s,fieldIds:c})||a.some(e=>Object.entries(e.values).some(([e,t])=>!Qn(o,i,e,t))))return!1;let l=ne(o.getSnapshot().tables[i],a),u=e.get(F),d=e.get(I),f=e.get(X).createSetRecordValuesOp(o,i,a),p=new k().invertOp(o.getSnapshot(),f),m=t.ganttPredecessor,h={unitId:r,op:f,...m&&!m.onUndo?{ganttPredecessorAddition:m.addition}:{},trigger:l.length?nr.id:void 0,hierarchyChanges:l.length?l:void 0,source:l.length?t.source??`ui`:void 0},g={...h,source:l.length?`redo`:void 0},_={unitId:r,op:p,...m!=null&&m.onUndo?{ganttPredecessorAddition:m.addition}:{},trigger:l.length?nr.id:void 0,hierarchyChanges:l.length?l.map(e=>({...e,oldParentRecordId:e.parentRecordId,parentRecordId:e.oldParentRecordId})):void 0,source:l.length?`undo`:void 0};return u.syncExecuteCommand(A.id,h)?(n.focusUnit(r),d.pushUndoRedo({unitID:r,undoMutations:[{id:A.id,params:_}],redoMutations:[{id:A.id,params:g}]}),!0):!1}},rr={id:`base.command.add-gantt-predecessor`,type:P.COMMAND,handler(e,t){return ar(e,t,!1)}},ir={id:`base.command.remove-gantt-predecessor`,type:P.COMMAND,handler(e,t){return ar(e,t,!0)===!0}};function ar(e,t,n){var r;if(!t)return!1;let{unitId:i,tableId:a,viewId:o,predecessorId:s,successorId:c}=t,l=e.get(L).getUnit(i,V.UNIVER_BASE),u=l==null?void 0:l.getSnapshot().tables[a],d=u==null?void 0:u.views[o],f=d==null||(r=d.config)==null?void 0:r.dependencyFieldId,p=f?u==null?void 0:u.fields[f]:void 0,m=u==null?void 0:u.records[c];if(!u||!m||!n&&!u.records[s]||(d==null?void 0:d.type)!==Be.Gantt||!p||!Te(a,p))return!1;let h=ke(m.values[p.id]),g=h.includes(s);if(n?!g:g)return!0;if(!n&&ye(s,c,e=>{var t;return(t=u.records[e])==null?void 0:t.values[p.id]}))return`cycle`;let _=n?h.filter(e=>e!==s):[...h,s],v=oe(_),y=[];for(let e of Object.values(u.records)){let t=n&&e.id===c?v:e.values[p.id];typeof t==`string`&&t&&y.push([e.id,t])}let b={tableId:a,fieldId:p.id,predecessorId:s,successorId:c,graph:y};return e.get(F).syncExecuteCommand(nr.id,{unitId:i,tableId:a,viewId:o,patches:[{recordId:c,values:{[p.id]:v}}],ganttPredecessor:{addition:b,onUndo:n}})}function or(e,t,n,r){let i=e.getSnapshot().tables[t];if(!i)return null;let a=_(i),o=a??_e(i),s=n.some(e=>Object.prototype.hasOwnProperty.call(e.values,o)),c=a?i.fields[a]:s?ee(i,o,r):null,l=[];for(let r of n){let n=r.values[o],i=c!==null&&Object.prototype.hasOwnProperty.call(r.values,o),a=tr(n),s=null;if(i){if(typeof a!=`string`||!le(a,!1).valid)return null;s=a}let u={};if(Object.entries(r.values).forEach(([e,t])=>{i&&e===o||(u[e]=t)}),!$n(e,t,{...r,values:u}))return null;let d={...r.values};s!==null&&(d[o]=s),l.push(er({...r,values:d}))}return{records:l,hierarchyFieldToMaterialize:a?null:c,hierarchyChanges:de(i,l)}}const sr={id:`base.command.batch-create-base-records`,type:P.COMMAND,handler(e,t){if(!t)return!1;let n=e.get(L),{unitId:r,tableId:i,records:a}=t,o=n.getUnit(r,V.UNIVER_BASE),s=a.map(e=>e.id);if(!o||!Z(o,i)||new Set(s).size!==s.length)return!1;let c=e.get(F),l=e.get(I),u=e.get(X),d=o.getSnapshot(),f=or(o,i,a);if(!f)return!1;let{records:m,hierarchyChanges:h,hierarchyFieldToMaterialize:g}=f,_=g?u.createCreateFieldOp(o,i,g,d.tables[i].fieldOrder.length):null,v=u.createBatchCreateRecordsOp(o,i,m),y=p([..._?[_]:[],v]),b=new k().invertOp(d,y),x={unitId:r,op:y,hierarchyChanges:h.length?h:void 0,source:h.length?t.source??`ui`:void 0},S={...x,source:h.length?`redo`:void 0},C={unitId:r,op:b,hierarchyChanges:h.length?h.map(e=>({...e,oldParentRecordId:e.parentRecordId,parentRecordId:null})):void 0,source:h.length?`undo`:void 0};return c.syncExecuteCommand(A.id,x)?(n.focusUnit(r),l.pushUndoRedo({unitID:r,undoMutations:[{id:A.id,params:C}],redoMutations:[{id:A.id,params:S}]}),!0):!1}},cr={id:`base.command.batch-delete-base-records`,type:P.COMMAND,handler:(e,t)=>{if(!t)return!1;let n=e.get(L),{unitId:r,tableId:i,recordIds:a}=t,o=n.getUnit(r,V.UNIVER_BASE);if(!o||!Z(o,i,{recordIds:a}))return!1;let s=e.get(F),c=e.get(I),l=o.getSnapshot(),u=T(l.tables[i],a),d=new K,f=new k,p=d.deleteRecords(l,i,a),m=f.applyOp(l,p),h=d.createDeleteRecordsUndoOp(l,m,i,a),g={unitId:r,op:p,hierarchyChanges:u,source:t.source??`ui`},_={...g,source:`redo`},v={unitId:r,op:h,hierarchyChanges:u.map(e=>({...e,oldParentRecordId:e.parentRecordId,parentRecordId:e.oldParentRecordId})),source:`undo`};return s.syncExecuteCommand(A.id,g)?(n.focusUnit(r),c.pushUndoRedo({unitID:r,undoMutations:[{id:A.id,params:v}],redoMutations:[{id:A.id,params:_}]}),!0):!1}},lr={id:`base.command.change-base-field-type`,type:P.COMMAND,handler:(e,t)=>{if(!t)return!1;let n=e.get(L),{unitId:r,tableId:i,fieldId:a,input:o}=t,s=n.getUnit(r,V.UNIVER_BASE);if(!s||a===j||!Z(s,i,{fieldIds:[a]}))return!1;let c=e.get(F),l=e.get(I),u=s.getSnapshot();if(!Zn(s,i,{...u.tables[i].fields[a],...o}))return!1;let d=new K().changeFieldType(u,i,a,o),f=new k().invertOp(u,d),p={unitId:r,op:d},m={unitId:r,op:f};return c.syncExecuteCommand(A.id,p)?(n.focusUnit(r),l.pushUndoRedo({unitID:r,undoMutations:[{id:A.id,params:m}],redoMutations:[{id:A.id,params:p}]}),!0):!1}},ur={id:`base.command.create-base-child-record`,type:P.COMMAND,handler(e,t){var n;if(!t)return!1;let{unitId:r,tableId:i,parentRecordId:a}=t,o=e.get(L),s=o.getUnit(r,V.UNIVER_BASE);if(!s||!Z(s,i,{recordIds:[a]}))throw new d(`missingRecord`);let c=s.getSnapshot(),l=c.tables[i],f=_(l),m=f??t.fieldId??_e(l);if(f&&t.fieldId&&t.fieldId!==f||l.fields[m]&&!se(l,m))throw new d(`invalidField`);if((u(l,m).depthByRecordId.get(a)??0)+1>=5)throw new d(`maxDepth`);if(!$n(s,i,t.record))return!1;let h=er({...t.record,values:{...t.record.values,[m]:oe([a])}}),g=e.get(X),v=new K,y=l.fields[m],b=y?((n=me(y))==null?void 0:n.relationRole)===N.Parent?null:v.updateField(c,i,m,{config:{...y.config,relationRole:N.Parent}}):g.createCreateFieldOp(s,i,ee(l,m,t.parentFieldName),l.fieldOrder.length),x=g.createCreateRecordOp(s,i,h),S=p([...b?[b]:[],x]),C=new k().invertOp(c,S),w={tableId:i,fieldId:m,recordId:h.id,oldParentRecordId:null,parentRecordId:a},T={unitId:r,op:S,trigger:ur.id,hierarchyChanges:[w],source:t.source??`ui`},E={...T,source:`redo`},te={unitId:r,op:C,trigger:ur.id,hierarchyChanges:[{...w,oldParentRecordId:a,parentRecordId:null}],source:`undo`};return e.get(F).syncExecuteCommand(A.id,T)?(o.focusUnit(r),e.get(I).pushUndoRedo({unitID:r,undoMutations:[{id:A.id,params:te}],redoMutations:[{id:A.id,params:E}]}),!0):!1}},dr={id:`base.command.create-base-field`,type:P.COMMAND,handler(e,t){if(!t)return!1;let n=e.get(L),{unitId:r,tableId:i,field:a,index:o,viewIndexes:s}=t,c=n.getUnit(r,V.UNIVER_BASE);if(!c||!Z(c,i)||c.getSnapshot().tables[i].fields[a.id]||o===0||!Zn(c,i,a))return!1;let l=e.get(F),u=e.get(I),d=e.get(X).createCreateFieldOp(c,i,a,o,s),f=new k().invertOp(c.getSnapshot(),d),p={unitId:r,op:d},m={unitId:r,op:f};return l.syncExecuteCommand(A.id,p)?(n.focusUnit(r),u.pushUndoRedo({unitID:r,undoMutations:[{id:A.id,params:m}],redoMutations:[{id:A.id,params:p}]}),!0):!1}},fr={id:`base.command.create-base-record`,type:P.COMMAND,handler(e,t){if(!t)return!1;let n=e.get(L),{unitId:r,tableId:i,record:a}=t,o=n.getUnit(r,V.UNIVER_BASE);if(!o||!Z(o,i))return!1;let s=e.get(F),c=e.get(I),l=e.get(X),u=o.getSnapshot(),d=or(o,i,[a]);if(!d)return!1;let[f]=d.records,{hierarchyChanges:m,hierarchyFieldToMaterialize:h}=d,g=h?l.createCreateFieldOp(o,i,h,u.tables[i].fieldOrder.length):null,_=l.createCreateRecordOp(o,i,f),v=p([...g?[g]:[],_]),y=new k().invertOp(u,v),b={unitId:r,op:v,hierarchyChanges:m.length?m:void 0,source:m.length?t.source??`ui`:void 0},x={...b,source:m.length?`redo`:void 0},S={unitId:r,op:y,hierarchyChanges:m.length?m.map(e=>({...e,oldParentRecordId:e.parentRecordId,parentRecordId:null})):void 0,source:m.length?`undo`:void 0};return s.syncExecuteCommand(A.id,b)?(n.focusUnit(r),c.pushUndoRedo({unitID:r,undoMutations:[{id:A.id,params:S}],redoMutations:[{id:A.id,params:x}]}),!0):!1}},pr=`Base table names are exported as Excel worksheet names and must contain 1-31 characters, not start or end with an apostrophe, not contain : \\ / ? * [ ], and be unique (case-insensitive).`;function mr(e,t,n){if(!t.trim())return{valid:!1,reason:`Table name is required.`,error:`required`};if(t.length>31)return{valid:!1,reason:`Use 31 characters or fewer.`,error:`too-long`};if(t.startsWith(`'`)||t.endsWith(`'`))return{valid:!1,reason:`A table name cannot start or end with an apostrophe.`,error:`apostrophe`};if(/[:\\/?*\[\]]/.test(t))return{valid:!1,reason:`A table name cannot contain : \\ / ? * [ ].`,error:`invalid-characters`};if(!mt(t))return{valid:!1,reason:`Choose another name.`,error:`invalid`};let r=t.toLowerCase();return Object.values(e.tables).some(e=>e.id!==n&&e.name.toLowerCase()===r)?{valid:!1,reason:`A table with this name already exists.`,error:`duplicate`}:{valid:!0}}function hr(e,t){let n=t.replace(/[:\\/?*\[\]]/g,` `).replace(/^'+|'+$/g,``).trim()||`Table`,r=1;for(;;){let t=r===1?``:` ${r}`,i=`${n.slice(0,31-t.length)}${t}`;if(mr(e,i).valid)return i;r++}}const gr={id:`base.command.create-base-table`,type:P.COMMAND,handler:(e,t)=>{if(!t)return!1;let n=e.get(L),{unitId:r,table:i,index:a}=t,o=n.getUnit(r,V.UNIVER_BASE);if(!o||o.getSnapshot().tables[i.id]||!mr(o.getSnapshot(),i.name).valid)return!1;try{at(i)}catch{return!1}let s=e.get(F),c=e.get(I),l=o.getSnapshot(),u=new K().createTable(l,i,a),d=new k().invertOp(l,u),f={unitId:r,op:u},p={unitId:r,op:d};return s.syncExecuteCommand(A.id,f)?(n.focusUnit(r),c.pushUndoRedo({unitID:r,undoMutations:[{id:A.id,params:p}],redoMutations:[{id:A.id,params:f}]}),!0):!1}};function _r(e,t,n=[]){let r=/* @__PURE__ */ new Set([...Object.values(e.fields).filter(e=>e).map(e=>e.name),...n.map(e=>e.name)]);if(!r.has(t))return t;let i=2;for(;r.has(`${t} ${i}`);)i++;return`${t} ${i}`}function vr(e,t){if(!e||!t.length)return;let n=[...e];return t.forEach(e=>{n.includes(e)||n.push(e)}),n}const yr={options:[{id:`todo`,name:`Todo`,color:`#BFDBFE`},{id:`doing`,name:`Doing`,color:`#FDE68A`},{id:`done`,name:`Done`,color:`#BBF7D0`}]},br={locale:`en-US`,pattern:`yyyy/mm/dd`,includeTime:!1,hourCycle:`h24`,defaultMode:`none`};function xr(e,t,n,r){var i;let a=new K,o=e.tables[t],s=o?{...n,fieldOrder:[j,...(n.fieldOrder??o.fieldOrder).filter(e=>e!==j)],fieldSettings:{...n.fieldSettings,[j]:{...(i=n.fieldSettings)==null?void 0:i[j],hidden:!0}}}:n;if(o&&s.type===`kanban`){let n=Cr(o,s),i=n.changed?{...s,config:n.config,fieldOrder:n.fieldOrder??s.fieldOrder}:s;return p([...n.fields.map(n=>a.createField(e,t,{field:n})),a.createView(e,t,i,r)])}if(!o||s.type!==`calendar`&&s.type!==`gantt`)return a.createView(e,t,s,r);let c=Sr(o,s),l=c.changed?{...s,config:c.config,fieldOrder:c.fieldOrder??s.fieldOrder}:s;return l.type===`gantt`&&(l={...l,config:{displayColor:{type:`custom`,color:`#3b82f6`},...l.config}},Er(s)&&(l=Dr(o,l,c.fields))),p([...c.fields.map(n=>a.createField(e,t,{field:n})),a.createView(e,t,l,r)])}function Sr(e,t,n=!1){var r,i;let a=e.fieldOrder.filter(t=>kr(e,t)),o=[],s={...t.config??{}},c=typeof s.startDateFieldId==`string`&&kr(e,s.startDateFieldId)?s.startDateFieldId:a[0],l=typeof s.endDateFieldId==`string`&&kr(e,s.endDateFieldId)&&s.endDateFieldId!==c?s.endDateFieldId:a.find(e=>e!==c);if(!c){let n=Ar(e,o,t.type===`gantt`?`Start date`:`Date`);o.push(n),c=n.id}if((t.type===`gantt`||n)&&!l){let t=Ar(e,o,`End date`);o.push(t),l=t.id}t.type===`calendar`&&!l&&delete s.endDateFieldId,s.startDateFieldId=c,t.type===`calendar`&&l&&(s.endDateFieldId=l),t.type===`gantt`&&(s.endDateFieldId=l??c);let u=vr(t.fieldOrder,o.map(e=>e.id));return{changed:o.length>0||s.startDateFieldId!==((r=t.config)==null?void 0:r.startDateFieldId)||s.endDateFieldId!==((i=t.config)==null?void 0:i.endDateFieldId)||!(!u||B.diffValue(u,t.fieldOrder??[])),fields:o,config:s,fieldOrder:u}}function Cr(e,t){var n;let r=[],i={...t.config??{}},a=typeof i.groupFieldId==`string`&&wr(e,i.groupFieldId)?i.groupFieldId:e.fieldOrder.find(t=>wr(e,t));if(!a){let t=Tr(e,r);r.push(t),a=t.id}i.groupFieldId=a;let o=vr(t.fieldOrder,r.map(e=>e.id));return{changed:r.length>0||i.groupFieldId!==((n=t.config)==null?void 0:n.groupFieldId)||!(!o||B.diffValue(o,t.fieldOrder??[])),fields:r,config:i,fieldOrder:o}}function wr(e,t){let n=t?e.fields[t]:null;return n?[M.SingleSelect,M.MultiSelect,M.Rating,M.Person,M.Group,M.Checkbox].includes(n.type):!1}function Tr(e,t){return{id:`fld-${lt(8)}`,name:_r(e,`Status`,t),type:M.SingleSelect,config:B.deepClone(yr)}}function Er(e){return Object.keys(e.config??{}).length===0&&Object.keys(e.fieldSettings??{}).length===0}function Dr(e,t,n){let r={...t.config??{}},i=n.map(e=>e.id),a=[...e.fieldOrder,...i],o=typeof r.titleFieldId==`string`&&Or(e,n,r.titleFieldId)?r.titleFieldId:e.primaryFieldId,s=typeof r.startDateFieldId==`string`?r.startDateFieldId:``,c=typeof r.endDateFieldId==`string`?r.endDateFieldId:s,l=new Set([o,s,c].filter(t=>t&&Or(e,n,t))),u=Object.fromEntries(a.flatMap(t=>l.has(t)||!Or(e,n,t)?[]:[[t,{hidden:!0}]]));return{...t,config:{...r,titleFieldId:o},fieldSettings:u}}function Or(e,t,n){return!!(e.fields[n]??t.find(e=>e.id===n))&&!0}function kr(e,t){let n=t?e.fields[t]:null;return!!n&&n.type===M.Date&&n.readonly!==!0&&n.system!==!0}function Ar(e,t,n){let r=/* @__PURE__ */ new Set([...Object.keys(e.fields),...t.map(e=>e.id)]),i=`fld-${lt(8)}`;for(;r.has(i);)i=`fld-${lt(8)}`;return{id:i,name:_r(e,n,t),type:M.Date,config:{...br}}}const jr={id:`base.command.create-base-view`,type:P.COMMAND,handler:(e,t)=>{if(!t)return!1;let n=e.get(L),{unitId:r,tableId:i,view:a,index:o}=t,s=n.getUnit(r,V.UNIVER_BASE);if(!s||!Z(s,i)||s.getSnapshot().tables[i].views[a.id])return!1;let c=e.get(F),l=e.get(I),u=s.getSnapshot(),d=xr(u,i,a,o),f=new k().invertOp(u,d),p={historyAction:`create-view`,unitId:r,op:d,viewType:a.type},m={historyAction:`delete-view`,unitId:r,op:f,viewType:a.type};return c.syncExecuteCommand(A.id,p)?(n.focusUnit(r),l.pushUndoRedo({unitID:r,undoMutations:[{id:A.id,params:m}],redoMutations:[{id:A.id,params:p}]}),!0):!1}},Mr={id:`base.command.delete-base-field`,type:P.COMMAND,handler(e,t){if(!t)return!1;let n=e.get(L),{unitId:r,tableId:i,fieldId:a}=t,o=n.getUnit(r,V.UNIVER_BASE);if(!o)return!1;let s=o.getSnapshot(),c=s.tables[i];if(!Z(o,i,{fieldIds:[a]})||c.primaryFieldId===a||a===j)return!1;let l=e.get(F),u=e.get(I),d=new K().deleteField(s,i,a),f=new k().invertOp(s,d),p={unitId:r,op:d},m={unitId:r,op:f};return l.syncExecuteCommand(A.id,p)?(n.focusUnit(r),u.pushUndoRedo({unitID:r,undoMutations:[{id:A.id,params:m}],redoMutations:[{id:A.id,params:p}]}),!0):!1}},Nr={id:`base.command.delete-base-record`,type:P.COMMAND,handler:(e,t)=>{if(!t)return!1;let n=e.get(L),{unitId:r,tableId:i,recordId:a}=t,o=n.getUnit(r,V.UNIVER_BASE);if(!o||!Z(o,i,{recordIds:[a]}))return!1;let s=e.get(F),c=e.get(I),l=o.getSnapshot(),u=T(l.tables[i],[a]),d=new K,f=new k,p=d.deleteRecord(l,i,a),m=f.applyOp(l,p),h=d.createDeleteRecordsUndoOp(l,m,i,[a]),g={unitId:r,op:p,hierarchyChanges:u,source:t.source??`ui`},_={...g,source:`redo`},v={unitId:r,op:h,hierarchyChanges:u.map(e=>({...e,oldParentRecordId:e.parentRecordId,parentRecordId:e.oldParentRecordId})),source:`undo`};return s.syncExecuteCommand(A.id,g)?(n.focusUnit(r),c.pushUndoRedo({unitID:r,undoMutations:[{id:A.id,params:v}],redoMutations:[{id:A.id,params:_}]}),!0):!1}},Pr={id:`base.command.delete-base-table`,type:P.COMMAND,handler:(e,t)=>{if(!t)return!1;let n=e.get(L),{unitId:r,tableId:i}=t,a=n.getUnit(r,V.UNIVER_BASE);if(!a||!Z(a,i))return!1;let o=e.get(F),s=e.get(I),c=a.getSnapshot(),l=ie(c,i);if(l.length>0)return console.error(`[DeleteBaseTableCommand]: table "${i}" is referenced by ${l.map(e=>`${e.tableId}.${e.fieldId}`).join(`, `)}.`),!1;let u=new K().deleteTable(c,i),d=new k().invertOp(c,u),f={unitId:r,op:u},p={unitId:r,op:d};return o.syncExecuteCommand(A.id,f)?(n.focusUnit(r),s.pushUndoRedo({unitID:r,undoMutations:[{id:A.id,params:p}],redoMutations:[{id:A.id,params:f}]}),!0):!1}},Fr={id:`base.command.delete-base-view`,type:P.COMMAND,handler:(e,t)=>{if(!t)return!1;let n=e.get(L),{unitId:r,tableId:i,viewId:a}=t,o=n.getUnit(r,V.UNIVER_BASE);if(!o||!Z(o,i,{viewIds:[a]}))return!1;let s=e.get(F),c=e.get(I),l=o.getSnapshot(),u=l.tables[i].views[a].type,d=new K().deleteView(l,i,a),f=new k().invertOp(l,d),p={historyAction:`delete-view`,unitId:r,op:d,viewType:u},m={historyAction:`create-view`,unitId:r,op:f,viewType:u};return s.syncExecuteCommand(A.id,p)?(n.focusUnit(r),c.pushUndoRedo({unitID:r,undoMutations:[{id:A.id,params:m}],redoMutations:[{id:A.id,params:p}]}),!0):!1}},Ir={id:`base.command.duplicate-base-field`,type:P.COMMAND,handler:(e,t)=>{if(!t)return!1;let n=e.get(L),{unitId:r,tableId:i,sourceFieldId:a,field:o,index:s,viewIndexes:c}=t,l=n.getUnit(r,V.UNIVER_BASE);if(!l||!Z(l,i,{fieldIds:[a]})||a===j||l.getSnapshot().tables[i].fields[o.id]||s===0||!Zn(l,i,o))return!1;let u=e.get(F),d=e.get(I),f=l.getSnapshot(),p=new K().duplicateField(f,i,a,{field:o,index:s,viewIndexes:c}),m=new k().invertOp(f,p),h={unitId:r,op:p},g={unitId:r,op:m};return u.syncExecuteCommand(A.id,h)?(n.focusUnit(r),d.pushUndoRedo({unitID:r,undoMutations:[{id:A.id,params:g}],redoMutations:[{id:A.id,params:h}]}),!0):!1}},Lr={id:`base.command.duplicate-base-record`,type:P.COMMAND,handler:(e,t)=>{if(!t)return!1;let n=e.get(L),{unitId:r,tableId:i,sourceRecordId:a,record:o}=t,s=n.getUnit(r,V.UNIVER_BASE);if(!s||!Z(s,i,{recordIds:[a]})||!$n(s,i,o))return!1;let c=e.get(F),l=e.get(I),u=s.getSnapshot(),d=er(o),f=de(u.tables[i],[d]),p=new K().duplicateRecord(u,i,a,d),m=new k().invertOp(u,p),h={unitId:r,op:p,hierarchyChanges:f.length?f:void 0,source:f.length?t.source??`ui`:void 0},g={...h,source:f.length?`redo`:void 0},_={unitId:r,op:m,hierarchyChanges:f.length?f.map(e=>({...e,oldParentRecordId:e.parentRecordId,parentRecordId:null})):void 0,source:f.length?`undo`:void 0};return c.syncExecuteCommand(A.id,h)?(n.focusUnit(r),l.pushUndoRedo({unitID:r,undoMutations:[{id:A.id,params:_}],redoMutations:[{id:A.id,params:g}]}),!0):!1}},Rr={id:`base.command.ensure-base-temporal-view-date-fields`,type:P.COMMAND,handler(e,t){if(!t)return!1;let n=e.get(L),{unitId:r,tableId:i,viewId:a,ensureEndDateField:o}=t,s=n.getUnit(r,V.UNIVER_BASE);if(!s||!Z(s,i,{viewIds:[a]}))return!1;let c=s.getSnapshot(),l=c.tables[i],u=l==null?void 0:l.views[a];if(!l||!u||u.type!==`calendar`&&u.type!==`gantt`)return!1;let d=Sr(l,u,o===!0);if(!d.changed)return!1;let f=e.get(F),m=e.get(I),h=new K,g=[...d.fields.map(e=>h.createField(c,t.tableId,{field:e})),zr([...Br(t.tableId,t.viewId),`config`],u.config,d.config)];d.fieldOrder&&g.push(zr([...Br(t.tableId,t.viewId),`fieldOrder`],u.fieldOrder,d.fieldOrder));let _=p(g),v=new k().invertOp(c,_),y={unitId:r,op:_},b={unitId:r,op:v};return f.syncExecuteCommand(A.id,y)?(n.focusUnit(r),m.pushUndoRedo({unitID:r,undoMutations:[{id:A.id,params:b}],redoMutations:[{id:A.id,params:y}]}),!0):!1}};function zr(e,t,n){return t===void 0?R.insertOp(e,n):R.replaceOp(e,t,n)}function Br(e,t){return[`tables`,e,`views`,t]}const Vr={id:`base.command.move-base-field`,type:P.COMMAND,handler:(e,t)=>{if(!t)return!1;let n=e.get(L),{unitId:r,tableId:i,fieldId:a,target:o}=t,s=n.getUnit(r,V.UNIVER_BASE),c=o.beforeFieldId??o.afterFieldId;if(!s||!!o.beforeFieldId==!!o.afterFieldId||!c||c===a||a===j||o.beforeFieldId===j||!Z(s,i,{fieldIds:[a,c]}))return!1;let l=e.get(F),u=e.get(I),d=s.getSnapshot(),f=new K().moveField(d,i,a,o),p=new k().invertOp(d,f),m={unitId:r,op:f},h={unitId:r,op:p};return l.syncExecuteCommand(A.id,m)?(n.focusUnit(r),u.pushUndoRedo({unitID:r,undoMutations:[{id:A.id,params:h}],redoMutations:[{id:A.id,params:m}]}),!0):!1}},Hr={id:`base.command.move-base-hierarchy-record`,type:P.COMMAND,handler:(e,t)=>{var n;if(!t)return!1;let{unitId:r,tableId:i,recordId:a,parentRecordId:o,orderKey:s}=t,c=e.get(L),l=c.getUnit(r,V.UNIVER_BASE);if(!l||!Z(l,i,{recordIds:[a,...o?[o]:[]]}))throw new d(`missingRecord`);let f=l.getSnapshot(),m=f.tables[i],h=_(m),g=h??t.fieldId??_e(m);if(h&&t.fieldId&&t.fieldId!==h||m.fields[g]&&!se(m,g))throw new d(`invalidField`);let v=u(m,g),y=we(v,a,o);if(!y.valid){let e=y.reason===ze.SelfParent?`selfParent`:y.reason===ze.Cycle?`cycle`:`maxDepth`;throw new d(e)}let b=v.parentByRecordId.get(a)??null,x=m.records[a].orderKey;if(b===o&&(s===void 0||s===x))return!0;let S=b!==o,C=s!==void 0&&s!==x,w=new K,T=[],E=m.fields[g],te=S&&(!E||((n=me(E))==null?void 0:n.relationRole)!==N.Parent)?E?w.updateField(f,i,g,{config:{...E.config,relationRole:N.Parent}}):e.get(X).createCreateFieldOp(l,i,ee(m,g,t.parentFieldName),m.fieldOrder.length):null;te&&T.push(te),S&&T.push(w.updateCell(f,i,a,g,o?oe([o]):``)),C&&T.push(w.updateRecordOrder(f,i,a,s));let ne=p(T),D={tableId:i,fieldId:g,recordId:a,oldParentRecordId:b,parentRecordId:o},re={unitId:r,op:ne,trigger:Hr.id,hierarchyChanges:[D],source:t.source??`ui`},ie={...re,source:`redo`};if(!e.get(F).syncExecuteCommand(A.id,re))return!1;let ae=l.getSnapshot(),ce=[];S&&ce.push(Ur(ae,f,i,a,g)),C&&ce.push(Wr(ae,f,i,a)),te&&ce.push(new k().invertOp(f,te));let le={unitId:r,op:p(ce),trigger:Hr.id,hierarchyChanges:[{...D,oldParentRecordId:o,parentRecordId:b}],source:`undo`};return c.focusUnit(r),e.get(I).pushUndoRedo({unitID:r,undoMutations:[{id:A.id,params:le}],redoMutations:[{id:A.id,params:ie}]}),!0}};function Ur(e,t,n,r,i){var a,o,s,c,l,u,d,f;let m=e.tables[n],h=t.tables[n],g=(a=m.rowIndex)==null?void 0:a[r],_=(o=m.colIndex)==null?void 0:o[i],v=(s=h.rowIndex)==null?void 0:s[r],y=(c=h.colIndex)==null?void 0:c[i],b=g==null||_==null||(l=m.cellData)==null||(l=l[g])==null?void 0:l[_],x=v==null||y==null||(u=h.cellData)==null||(u=u[v])==null?void 0:u[y],S=(d=m.records[r])==null?void 0:d.values[i],C=(f=h.records[r])==null?void 0:f.values[i],w=[],ee=Gr(g==null||_==null?null:G.cellData(n,g,_),b,x);ee&&w.push(ee);let T=Gr(G.cell(n,r,i),S,C);return T&&w.push(T),p(w)}function Wr(e,t,n,r){var i,a;let o=e.tables[n],s=t.tables[n],c=[Gr([...G.record(n,r),`orderKey`],(i=o.records[r])==null?void 0:i.orderKey,(a=s.records[r])==null?void 0:a.orderKey),Gr([...G.table(n),`recordOrder`],o.recordOrder,s.recordOrder)].filter(e=>!!e);return p(c)}function Gr(e,t,n){if(!e||t===void 0&&n===void 0)return null;let r=Qe.getInstance();return n===void 0?r.removeOp(e,t):t===void 0?r.insertOp(e,n):r.replaceOp(e,t,n)}const Kr={id:`base.command.move-base-view`,type:P.COMMAND,handler:(e,t)=>{if(!t)return!1;let n=e.get(L),{unitId:r,tableId:i,viewId:a,target:o}=t,s=n.getUnit(r,V.UNIVER_BASE),c=o.beforeViewId??o.afterViewId;if(!s||!c||c===a||!Z(s,i,{viewIds:[a,c]}))return!1;let l=e.get(F),u=e.get(I),d=s.getSnapshot(),f=new K().moveView(d,i,a,o),p=new k().invertOp(d,f),m={unitId:r,op:f},h={unitId:r,op:p};return l.syncExecuteCommand(A.id,m)?(n.focusUnit(r),u.pushUndoRedo({unitID:r,undoMutations:[{id:A.id,params:h}],redoMutations:[{id:A.id,params:m}]}),!0):!1}},qr={id:`base.command.rename-base-table`,type:P.COMMAND,handler:(e,t)=>{if(!t)return!1;let n=e.get(L),{unitId:r,tableId:i,name:a}=t,o=n.getUnit(r,V.UNIVER_BASE);if(!o||!Z(o,i)||!mr(o.getSnapshot(),a,i).valid)return!1;let s=e.get(F),c=e.get(I),l=o.getSnapshot(),u=new K().renameTable(l,i,a),d=new k().invertOp(l,u),f={unitId:r,op:u},p={unitId:r,op:d};return s.syncExecuteCommand(A.id,f)?(n.focusUnit(r),c.pushUndoRedo({unitID:r,undoMutations:[{id:A.id,params:p}],redoMutations:[{id:A.id,params:f}]}),!0):!1}},Jr={id:`base.command.rename-base-view`,type:P.COMMAND,handler:(e,t)=>{if(!t)return!1;let n=e.get(L),{unitId:r,tableId:i,viewId:a,name:o}=t,s=n.getUnit(r,V.UNIVER_BASE);if(!s||!Z(s,i,{viewIds:[a]}))return!1;let c=e.get(F),l=e.get(I),u=s.getSnapshot(),d=new K().renameView(u,i,a,o),f=new k().invertOp(u,d),p={unitId:r,op:d},m={unitId:r,op:f};return c.syncExecuteCommand(A.id,p)?(n.focusUnit(r),l.pushUndoRedo({unitID:r,undoMutations:[{id:A.id,params:m}],redoMutations:[{id:A.id,params:p}]}),!0):!1}},Yr={id:`base.command.set-base-kanban-group-field`,type:P.COMMAND,handler:(e,t)=>{if(!t)return!1;let n=e.get(L),{unitId:r,tableId:i,viewId:a,fieldId:o}=t,s=n.getUnit(r,V.UNIVER_BASE);if(!s||!Z(s,i,{viewIds:[a],fieldIds:[o]}))return!1;let c=e.get(F),l=e.get(I),u=s.getSnapshot(),d=new K().updateViewConfig(u,i,a,{groupFieldId:o}),f=new k().invertOp(u,d),p={unitId:r,op:d},m={unitId:r,op:f};return c.syncExecuteCommand(A.id,p)?(n.focusUnit(r),l.pushUndoRedo({unitID:r,undoMutations:[{id:A.id,params:m}],redoMutations:[{id:A.id,params:p}]}),!0):!1}},Xr={id:`base.command.set-base-name`,type:P.COMMAND,handler:(e,t)=>{if(!t)return!1;let n=e.get(L),r=n.getUnit(t.unitId,V.UNIVER_BASE);if(!r)return!1;let i=r.getSnapshot().name;if(i===t.name)return!0;let a=new K,o=new k,s=[],c=[],l=[r,...n.getAllUnitsForType(V.UNIVER_BASE).filter(e=>e.getUnitId()!==t.unitId)];for(let e of l){let n=e.getSnapshot(),l=e===r?a.setBaseName(n,t.name):a.renameFormulaUnitReferences(n,i,t.name);l&&(s.push({id:A.id,params:{unitId:e.getUnitId(),op:l,trigger:Xr.id}}),c.unshift({id:A.id,params:{unitId:e.getUnitId(),op:o.invertOp(n,l),trigger:Xr.id}}))}let u=e.get(F),d=e.get(I),f=d.__tempBatchingUndoRedo(t.unitId);try{return gt(s,u).result?(n.focusUnit(t.unitId),d.pushUndoRedo({unitID:t.unitId,undoMutations:c,redoMutations:s}),!0):!1}finally{f.dispose()}}},Zr={type:P.COMMAND,id:`base.command.set-permission`,async handler(e,t){if(!t||!Qt.includes(t.action)||t.objectId!==t.unitId&&t.action!==U.Edit||t.policy&&(![`all`,`owner`,`members`].includes(t.policy.edit)||t.policy.strategies.some(e=>!Qt.includes(e.action)||t.objectId!==t.unitId&&e.action!==U.Edit)))return!1;let n=cn(t.unitId,t.objectId,t.action),r={unitId:t.unitId,objectId:t.objectId,objectType:n.type},i=e.get(tt);return t.remove?await i.remove(r):t.policy?await i.save(r,t.policy):await i.setPoint(r,n,t.value),!0}},Qr={type:P.COMMAND,id:`base.command.set-permissions`,async handler(e,t){if(!(t!=null&&t.unitId)||!Array.isArray(t.changes)||!t.changes.length||t.changes.some(e=>!(e!=null&&e.objectId)||e.objectId===t.unitId||e.policy!==null&&(!e.policy||![`all`,`owner`,`members`].includes(e.policy.edit)||!Array.isArray(e.policy.collaborators)||!Array.isArray(e.policy.strategies)||e.policy.strategies.some(e=>e.action!==U.Edit))))throw Error(`Invalid object permission batch.`);return e.get(tt).saveMany(t.changes.map(e=>{let n=cn(t.unitId,e.objectId,U.Edit);return{unitId:t.unitId,objectId:e.objectId,objectType:n.type,policy:e.policy}}))}};function $r(e,t){let r=m(B.deepClone(e)),i=/* @__PURE__ */ new Map;return t.forEach(e=>{var t,a;let o=`recordId`in e?e.recordId:(t=r.rowId)==null?void 0:t[e.row],s=`fieldId`in e?e.fieldId:(a=r.colId)==null?void 0:a[e.col],c=s?r.fields[s]:void 0;if(!o||!s||!c||!r.records[o])return;let l=n(e.value,c),u=S(e.value,l,c,r,o),d=i.get(o)??{};d[s]=u,i.set(o,d)}),[...i].map(([e,t])=>({recordId:e,values:t}))}const ei={id:`base.command.set-base-range-values`,type:P.COMMAND,handler(e,t){if(!t)return!1;let n=e.get(L),{unitId:r,tableId:i,patches:a}=t,o=n.getUnit(r,V.UNIVER_BASE);if(!o||!Z(o,i))return!1;let s=e.get(F),c=e.get(I),l=o.getSnapshot(),u=m(l.tables[i]),d=a.map(e=>{var t;return`recordId`in e?e.recordId:(t=u.rowId)==null?void 0:t[e.row]}).filter(e=>e!=null),f=a.map(e=>{var t;return`fieldId`in e?e.fieldId:(t=u.colId)==null?void 0:t[e.col]}).filter(e=>e!=null);if(d.length!==a.length||f.length!==a.length||!Z(o,i,{recordIds:d,fieldIds:f})||a.some((e,t)=>!Qn(o,i,f[t],e.value)))return!1;let p=ne(u,$r(u,a)),h=a.flatMap(e=>{var t,n;if(`row`in e)return[e];let r=(t=u.rowIndex)==null?void 0:t[e.recordId],i=(n=u.colIndex)==null?void 0:n[e.fieldId];return r==null||i==null?[]:[{row:r,col:i,value:e.value}]}),g=new K().setRangeValues(l,i,h),_=new k().invertOp(l,g),v={unitId:r,op:g,trigger:p.length?ei.id:void 0,hierarchyChanges:p.length?p:void 0,source:p.length?`redo`:void 0},y={unitId:r,op:_,trigger:p.length?ei.id:void 0,hierarchyChanges:p.length?p.map(e=>({...e,oldParentRecordId:e.parentRecordId,parentRecordId:e.oldParentRecordId})):void 0,source:p.length?`undo`:void 0},b={...v,source:p.length?t.source??`ui`:void 0};return s.syncExecuteCommand(A.id,b)?(n.focusUnit(r),c.pushUndoRedo({unitID:r,undoMutations:[{id:A.id,params:y}],redoMutations:[{id:A.id,params:v}]}),!0):!1}},ti={id:`base.command.set-base-table-hierarchy-field`,type:P.COMMAND,handler:(e,t)=>{if(!t)return!1;let{unitId:n,tableId:r,fieldId:i}=t,a=e.get(L),o=a.getUnit(n,V.UNIVER_BASE);if(!o||!Z(o,r))return!1;let s=o.getSnapshot(),c=s.tables[r];if(i!==null&&!se(c,i))throw new d(`invalidField`);if(_(c)===i)return!0;let l=new K,u=Object.values(c.fields).flatMap(e=>{if(!se(c,e.id))return[];let t=me(e);if(!t)return[];let n=e.id===i;if(t.relationRole===N.Parent===n)return[];let a={...e.config,targetTableId:t.targetTableId,multiple:t.multiple};return n?a.relationRole=N.Parent:delete a.relationRole,[l.updateField(s,r,e.id,{config:a})]}),f=p(u),m=new k().invertOp(s,f),h={unitId:n,op:f},g={unitId:n,op:m};return e.get(F).syncExecuteCommand(A.id,h)?(a.focusUnit(n),e.get(I).pushUndoRedo({unitID:n,undoMutations:[{id:A.id,params:g}],redoMutations:[{id:A.id,params:h}]}),!0):!1}},ni={id:`base.command.set-base-view-field-order`,type:P.COMMAND,handler:(e,t)=>{if(!t)return!1;let n=e.get(L),{unitId:r,tableId:i,viewId:a,fieldId:o,target:s}=t,c=n.getUnit(r,V.UNIVER_BASE),l=s.beforeFieldId??s.afterFieldId;if(!c||!l||l===o||!Z(c,i,{viewIds:[a],fieldIds:[o,l]}))return!1;let u=c.getSnapshot().tables[i],d=u.fields[o],f=u.fields[l];if(d.system===!0||d.type===M.RecordId||f.system===!0||f.type===M.RecordId)return!1;let p=e.get(F),m=e.get(I),h=c.getSnapshot(),g=new K().setViewFieldOrder(h,i,a,o,s),_=new k().invertOp(h,g),v={unitId:r,op:g},y={unitId:r,op:_};return p.syncExecuteCommand(A.id,v)?(n.focusUnit(r),m.pushUndoRedo({unitID:r,undoMutations:[{id:A.id,params:y}],redoMutations:[{id:A.id,params:v}]}),!0):!1}},ri={id:`base.command.set-base-view-field-visible`,type:P.COMMAND,handler(e,t){if(!t)return!1;let n=e.get(L),{unitId:r,tableId:i,viewId:a,fieldId:o,visible:s}=t,c=n.getUnit(r,V.UNIVER_BASE);if(!c||!Z(c,i,{viewIds:[a],fieldIds:[o]}))return!1;let l=c.getSnapshot().tables[i].fields[o];if(l.system===!0||l.type===M.RecordId)return!1;let u=e.get(F),d=e.get(I),f=c.getSnapshot(),p=new K,m=p.setViewFieldVisible(f,i,a,o,s),{undo:h,redo:g}=p.getViewFieldSettingHistory(f,i,a,o,{hidden:!s}),_={unitId:r,op:g},v={unitId:r,op:h};return u.syncExecuteCommand(A.id,{unitId:r,op:m})?(n.focusUnit(r),d.pushUndoRedo({unitID:r,undoMutations:[{id:A.id,params:v}],redoMutations:[{id:A.id,params:_}]}),!0):!1}},ii={id:`base.command.set-base-view-field-width`,type:P.COMMAND,handler:(e,t)=>{if(!t)return!1;let n=e.get(L),{unitId:r,tableId:i,viewId:a,fieldId:o,width:s}=t,c=n.getUnit(r,V.UNIVER_BASE);if(!c||!Z(c,i,{viewIds:[a],fieldIds:[o]}))return!1;let l=c.getSnapshot().tables[i].fields[o];if(l.system===!0||l.type===M.RecordId)return!1;let u=e.get(F),d=e.get(I),f=c.getSnapshot(),p=new K,m=p.setViewFieldWidth(f,i,a,o,s),{undo:h,redo:g}=p.getViewFieldSettingHistory(f,i,a,o,{width:s}),_={unitId:r,op:g},v={unitId:r,op:h};return u.syncExecuteCommand(A.id,{unitId:r,op:m})?(n.focusUnit(r),d.pushUndoRedo({unitID:r,undoMutations:[{id:A.id,params:v}],redoMutations:[{id:A.id,params:_}]}),!0):!1}},ai={id:`base.command.set-base-view-filter`,type:P.COMMAND,handler:(e,t)=>{if(!t)return!1;let n=e.get(L),{unitId:r,tableId:i,viewId:a,filter:o}=t,s=n.getUnit(r,V.UNIVER_BASE),c=(o==null?void 0:o.conditions.map(e=>e.fieldId))??[];if(!s||!Z(s,i,{viewIds:[a],fieldIds:c}))return!1;let l=e.get(F),u=e.get(I),d=s.getSnapshot(),f=new K().setViewFilter(d,i,a,o),p=new k().invertOp(d,f),m={unitId:r,op:f},h={unitId:r,op:p};return l.syncExecuteCommand(A.id,m)?(n.focusUnit(r),u.pushUndoRedo({unitID:r,undoMutations:[{id:A.id,params:h}],redoMutations:[{id:A.id,params:m}]}),!0):!1}},oi={id:`base.command.set-base-view-group`,type:P.COMMAND,handler:(e,t)=>{if(!t)return!1;let n=e.get(L),{unitId:r,tableId:i,viewId:a,group:o}=t,s=n.getUnit(r,V.UNIVER_BASE);if(!s||!Z(s,i,{viewIds:[a],fieldIds:o.map(e=>e.fieldId)}))return!1;let c=e.get(F),l=e.get(I),u=s.getSnapshot(),d=new K().setViewGroup(u,i,a,o),f=new k().invertOp(u,d),p={unitId:r,op:d},m={unitId:r,op:f};return c.syncExecuteCommand(A.id,p)?(n.focusUnit(r),l.pushUndoRedo({unitID:r,undoMutations:[{id:A.id,params:m}],redoMutations:[{id:A.id,params:p}]}),!0):!1}},si={id:`base.command.set-base-view-sort`,type:P.COMMAND,handler:(e,t)=>{if(!t)return!1;let n=e.get(L),{unitId:r,tableId:i,viewId:a,sort:o}=t,s=n.getUnit(r,V.UNIVER_BASE);if(!s||!Z(s,i,{viewIds:[a],fieldIds:o.map(e=>e.fieldId)}))return!1;let c=e.get(F),l=e.get(I),u=s.getSnapshot(),d=new K().setViewSort(u,i,a,o),f=new k().invertOp(u,d),p={unitId:r,op:d},m={unitId:r,op:f};return c.syncExecuteCommand(A.id,p)?(n.focusUnit(r),l.pushUndoRedo({unitID:r,undoMutations:[{id:A.id,params:m}],redoMutations:[{id:A.id,params:p}]}),!0):!1}},ci={id:`base.command.update-base-cell`,type:P.COMMAND,handler(e,t){if(!t)return!1;let n=e.get(L),{unitId:r,tableId:i,recordId:a,fieldId:o,value:s}=t,c=n.getUnit(r,V.UNIVER_BASE);if(!c||!Z(c,i,{recordIds:[a],fieldIds:[o]})||!Qn(c,i,o,s))return!1;let l=c.getSnapshot().tables[i],f=se(l,o)&&w(l).includes(o),p=f?u(l,o):null,m=(p==null?void 0:p.parentByRecordId.get(a))??null,h=f?ke(s)[0]??null:null;if(p){if(h&&!l.records[h])throw new d(`missingRecord`);let e=we(p,a,h);if(!e.valid){let t=e.reason===ze.SelfParent?`selfParent`:e.reason===ze.Cycle?`cycle`:`maxDepth`;throw new d(t)}}let g=e.get(F),_=e.get(I),v=e.get(X),y;try{y=v.createUpdateCellOp(c,i,a,o,s)}catch(e){return console.error(`Failed to create update cell operation:`,e),!1}let b=new k().invertOp(c.getSnapshot(),y),x=f&&m!==h?[{tableId:i,fieldId:o,recordId:a,oldParentRecordId:m,parentRecordId:h}]:void 0,S={unitId:r,op:y,trigger:x?ci.id:void 0,hierarchyChanges:x,source:x?t.source??`ui`:void 0},C={...S,source:x?`redo`:void 0},ee={unitId:r,op:b,trigger:x?ci.id:void 0,hierarchyChanges:x==null?void 0:x.map(e=>({...e,oldParentRecordId:e.parentRecordId,parentRecordId:e.oldParentRecordId})),source:x?`undo`:void 0};return g.syncExecuteCommand(A.id,S)?(n.getFocusedUnit()||n.focusUnit(r),_.pushUndoRedo({unitID:r,undoMutations:[{id:A.id,params:ee}],redoMutations:[{id:A.id,params:C}]}),!0):!1}},li={id:`base.command.update-base-field-description`,type:P.COMMAND,handler(e,t){if(!t)return!1;let n=e.get(L),{unitId:r,tableId:i,fieldId:a}=t,o=n.getUnit(r,V.UNIVER_BASE);if(!o||!Z(o,i,{fieldIds:[a]}))return!1;let s=e.get(F),c=e.get(I),l=o.getSnapshot(),u=t.description.trim(),d=new K().updateField(l,i,a,{description:u}),f=new k().invertOp(o.getSnapshot(),d),p={unitId:r,op:d},m={unitId:r,op:f};return s.syncExecuteCommand(A.id,p)?(n.focusUnit(r),c.pushUndoRedo({unitID:r,undoMutations:[{id:A.id,params:m}],redoMutations:[{id:A.id,params:p}]}),!0):!1}},ui={id:`base.command.update-base-field`,type:P.COMMAND,handler:(e,t)=>{if(!t)return!1;let n=e.get(L),{unitId:r,tableId:i,fieldId:a,patch:o}=t,s=n.getUnit(r,V.UNIVER_BASE);if(!s||a===j||!Z(s,i,{fieldIds:[a]}))return!1;let c=e.get(F),l=e.get(I),u=s.getSnapshot();if(!Zn(s,i,{...u.tables[i].fields[a],...o}))return!1;let d=new K().updateField(u,i,a,o),f=new k().invertOp(u,d),p={unitId:r,op:d},m={unitId:r,op:f};return c.syncExecuteCommand(A.id,p)?(n.focusUnit(r),l.pushUndoRedo({unitID:r,undoMutations:[{id:A.id,params:m}],redoMutations:[{id:A.id,params:p}]}),!0):!1}},di={id:`base.command.update-base-kanban-config`,type:P.COMMAND,handler:(e,t)=>{if(!t)return!1;let n=e.get(L),{unitId:r,tableId:i,viewId:a,patch:o}=t,s=n.getUnit(r,V.UNIVER_BASE);if(!s||!Z(s,i,{viewIds:[a]}))return!1;let c=e.get(F),l=e.get(I),u=s.getSnapshot(),d=new K().updateViewConfig(u,i,a,o),f=new k().invertOp(u,d),p={unitId:r,op:d},m={unitId:r,op:f};return c.syncExecuteCommand(A.id,p)?(n.focusUnit(r),l.pushUndoRedo({unitID:r,undoMutations:[{id:A.id,params:m}],redoMutations:[{id:A.id,params:p}]}),!0):!1}},fi={id:`base.command.update-base-record-order`,type:P.COMMAND,handler:(e,t)=>{if(!t)return!1;let n=e.get(L),{unitId:r,tableId:i,recordId:a,orderKey:o}=t,s=n.getUnit(r,V.UNIVER_BASE);if(!s||!Z(s,i,{recordIds:[a]}))return!1;let c=e.get(F),l=e.get(I),u=s.getSnapshot(),d=new K().updateRecordOrder(u,i,a,o),f=new k().invertOp(u,d),p={unitId:r,op:d},m={unitId:r,op:f};return c.syncExecuteCommand(A.id,p)?(n.focusUnit(r),l.pushUndoRedo({unitID:r,undoMutations:[{id:A.id,params:m}],redoMutations:[{id:A.id,params:p}]}),!0):!1}};let pi=/* @__PURE__ */ function(e){return e.Set=`set`,e.Add=`add`,e.Delete=`delete`,e.Clear=`clear`,e}({});const mi={id:`base.command.update-base-view-conditional-color-rules`,type:P.COMMAND,handler:(e,t)=>{var n;if(!t)return!1;let r=e.get(L),i=r.getUnit(t.unitId,V.UNIVER_BASE);if(!i||!Z(i,t.tableId,{viewIds:[t.viewId]}))return!1;let a=i.getSnapshot(),o=a.tables[t.tableId],s=hi(((n=o.views[t.viewId].config.conditionalColoring)==null?void 0:n.rules)??[],t);if(!s)return!1;gi(o,s);let c=new K().updateViewConfig(a,t.tableId,t.viewId,{conditionalColoring:s.length?{rules:s}:null}),l=new k().invertOp(a,c),u={unitId:t.unitId,op:c},d={unitId:t.unitId,op:l};return e.get(F).syncExecuteCommand(A.id,u)?(r.focusUnit(t.unitId),e.get(I).pushUndoRedo({unitID:t.unitId,undoMutations:[{id:A.id,params:d}],redoMutations:[{id:A.id,params:u}]}),!0):!1}};function hi(e,t){switch(t.type){case`set`:return B.deepClone(t.rules);case`add`:return[...B.deepClone(e),B.deepClone(t.rule)];case`delete`:{let n=e.filter(e=>e.id!==t.ruleId);return n.length===e.length?null:B.deepClone(n)}case`clear`:return e.length?[]:null}}function gi(e,t){if(!Array.isArray(t))throw TypeError(`[UpdateBaseViewConditionalColorRulesCommand]: rules must be an array.`);let n=/* @__PURE__ */ new Set;for(let[r,i]of t.entries())vi(e,i,_i(i,r,n))}function _i(e,t,n){if(!e||typeof e!=`object`)throw TypeError(`[UpdateBaseViewConditionalColorRulesCommand]: rule at index ${t} must be an object.`);let r=typeof e.id==`string`&&e.id.trim()?`"${e.id}"`:`at index ${t}`;if(typeof e.id!=`string`||!e.id.trim())throw Error(`[UpdateBaseViewConditionalColorRulesCommand]: rule ${r} must have a non-empty id.`);if(n.has(e.id))throw Error(`[UpdateBaseViewConditionalColorRulesCommand]: duplicate rule id "${e.id}".`);n.add(e.id);let i=typeof e.color==`string`&&e.color.trim()?new He(e.color):null;if(!(i!=null&&i.isValid))throw Error(`[UpdateBaseViewConditionalColorRulesCommand]: rule ${r} must have a valid CSS color.`);return r}function vi(e,t,n){let i=new Set(Object.values(Ie)),a=new Set(Object.values(Fe)),o=new Set(Object.values(Le));if(!i.has(t.target))throw Error(`[UpdateBaseViewConditionalColorRulesCommand]: rule ${n} has unsupported target "${String(t.target)}".`);let s=typeof t.fieldId==`string`?e.fields[t.fieldId]:void 0;if(!s||s.system===!0||s.type===M.RecordId)throw Error(`[UpdateBaseViewConditionalColorRulesCommand]: rule ${n} references missing or non-public field "${String(t.fieldId)}".`);if(!a.has(t.operator))throw Error(`[UpdateBaseViewConditionalColorRulesCommand]: rule ${n} has unsupported operator "${String(t.operator)}".`);if(t.dateMode!==void 0&&!o.has(t.dateMode))throw Error(`[UpdateBaseViewConditionalColorRulesCommand]: rule ${n} has unsupported date mode "${String(t.dateMode)}".`);if(t.target===Ie.COLUMN)return;let c=y(s.type);if(!c.includes(t.operator))throw Error(`[UpdateBaseViewConditionalColorRulesCommand]: rule ${n} cannot use operator "${t.operator}" with field type "${s.type}". Allowed operators: ${c.join(`, `)}.`);if(t.dateMode!==void 0&&!r(s.type))throw Error(`[UpdateBaseViewConditionalColorRulesCommand]: rule ${n} can use dateMode only with Date, CreatedAt, or UpdatedAt fields.`)}const yi={id:`base.command.update-base-view-config`,type:P.COMMAND,handler:(e,t)=>{if(!t)return!1;let n=e.get(L),{unitId:r,tableId:i,viewId:a,patch:o}=t,s=n.getUnit(r,V.UNIVER_BASE);if(!s||!Z(s,i,{viewIds:[a]}))return!1;let c=e.get(F),l=e.get(I),u=s.getSnapshot(),d=new K().updateViewConfig(u,i,a,o),f=new k().invertOp(u,d),p={unitId:r,op:d},m={unitId:r,op:f};return c.syncExecuteCommand(A.id,p)?(n.focusUnit(r),l.pushUndoRedo({unitID:r,undoMutations:[{id:A.id,params:m}],redoMutations:[{id:A.id,params:p}]}),!0):!1}},bi={id:`base.command.validate-formula`,type:P.COMMAND,handler(e,t){if(!t)return{valid:!1,reason:`[BaseFormula]: validation parameters are required.`};let n=e.get(L).getUnit(t.unitId,V.UNIVER_BASE),r=n==null?void 0:n.getSnapshot(),i=r==null?void 0:r.tables[t.tableId];return!r||!i?{valid:!1,reason:`[BaseFormula]: table does not exist.`}:new Mn().validateFormula(t.formula,i,{snapshot:r})}};let xi=class extends et{constructor(e){super(e,`BASE_OBJECT_PERMISSION_PLUGIN`,V.UNIVER_BASE,[W.BaseTable,W.BaseField,W.BaseRecord,W.BaseView,W.BaseDashboard])}};xi=O([E(0,Ye)],xi);const Si={id:`base.mutation.set-permission-rule`,type:P.MUTATION,handler:(e,t)=>!t||!e.get(L).getUnit(t.unitId,V.UNIVER_BASE)?!1:e.get(xi).setRule(t.unitId,t.objectType,t.objectId,t.rule)},Ci={id:`base.mutation.set-permission-rules`,type:P.MUTATION,handler:(e,t)=>!t||!e.get(L).getUnit(t.unitId,V.UNIVER_BASE)?!1:e.get(xi).setRules(t.unitId,t.rules)};function wi(e){let t=e.now??Date.now(),n=e.primaryFieldId??`${e.id}-field-name`,r=e.gridViewId??`${e.id}-view-grid`,i=e.recordCount??0,a=st(),o={id:n,name:e.primaryFieldName??`Name`,type:M.Text,config:{placeholder:`bases.fieldConfig.textPlaceholder`}},s={},c=[],l={},u={},d={};for(let r=0;r<i;r++){let i=`${e.id}-record-${r+1}`;s[i]={id:i,values:{[j]:i,...e.recordNamePrefix?{[n]:`${e.recordNamePrefix} ${r+1}`}:{}},orderKey:String(r+1).padStart(4,`0`),createdAt:t,updatedAt:t},c.push(i),l[i]=r,u[r]=i,d[r]={0:{v:i,t:Ve.STRING}},e.recordNamePrefix&&(d[r][1]={v:`${e.recordNamePrefix} ${r+1}`,t:Ve.STRING})}let f={id:r,tableId:e.id,name:`Grid`,type:Be.Grid,fieldOrder:[j,n],fieldSettings:{[j]:{hidden:!0}},config:{frozenFieldCount:1}};return{id:e.id,name:e.name,primaryFieldId:n,fieldOrder:[j,n],fields:{[j]:a,[n]:o},records:s,recordOrder:c,rowIndex:l,rowId:u,colIndex:{[j]:0,[n]:1},colId:{0:j,1:n},cellData:d,views:{[r]:f},viewOrder:[r]}}const Ti=`UNIVER_EMBED_BASES_TABLE`;function Ei(e){let t=wi({id:e.hostAnchorId,name:e.name??e.embedId,recordCount:0});return{...t,resources:{...t.resources??{},[Ti]:ki(e)}}}function Di(e){return{id:A.id,params:{unitId:e.unitId,op:R.type.compose(R.insertOp([`tables`,e.table.id],e.table),R.insertOp([`tableOrder`,e.index??e.snapshot.tableOrder.length],e.table.id)),trigger:`embed-ui`}}}function Oi(e){let t=e.snapshot.tableOrder.indexOf(e.tableId);if(!(!e.snapshot.tables[e.tableId]||t<0))return{id:A.id,params:{unitId:e.unitId,op:R.type.compose(R.removeOp([`tables`,e.tableId],e.snapshot.tables[e.tableId]),R.removeOp([`tableOrder`,t],e.tableId)),trigger:`embed-ui`}}}function ki(e){return{version:1,embedId:e.embedId,hostAnchorId:e.hostAnchorId,childUnitId:e.childUnitId,childType:e.childType}}function Ai(e){let t=Math.max(0,Number(e.decimalPlaces??0)),n=e.useThousands===!1?`0`:`#,##0`,r=t?`.${`0`.repeat(t)}`:``,i=Mi(e.abbreviation);return`${e.currencySymbol?Ni(e.currencySymbol):``}${n}${r}${i}`}function ji(e,t){let n=Ai(t),r=ht.format(n,e,{throws:!1});return Pi(String(r),t.separatorStyle,t.locale)}function Mi(e){return e===`K`?`,"K"`:e===`M`?`,,"M"`:e===`B`?`,,,"B"`:``}function Ni(e){return/^[A-Z]+$/i.test(e)?`"${e}"`:e}function Pi(e,t,n){let r=t===`local`?Fi(n):t;return r===`periodComma`?e.replace(/,/g,`\0`).replace(/\./g,`,`).replace(/\0/g,`.`):r===`spaceComma`?e.replace(/,/g,` `).replace(/\./g,`,`):r===`spacePeriod`?e.replace(/,/g,` `):e}function Fi(e){var t,n;let r=e||new Intl.NumberFormat().resolvedOptions().locale,i;try{i=new Intl.NumberFormat(r,{minimumFractionDigits:2,maximumFractionDigits:2,useGrouping:!0}).formatToParts(1e6)}catch{return`commaPeriod`}let a=(t=i.find(e=>e.type===`group`))==null?void 0:t.value,o=(n=i.find(e=>e.type===`decimal`))==null?void 0:n.value,s=a===`\xA0`||a===` `?` `:a;return s===`.`&&o===`,`?`periodComma`:s===` `&&o===`,`?`spaceComma`:s===` `&&o===`.`?`spacePeriod`:`commaPeriod`}const Ii=[`count`,`filled`,`empty`,`unique`,`sum`,`average`,`min`,`max`],Li=[`count`,`filled`,`empty`,`unique`],Ri=[`sum`,`average`,`min`,`max`],zi=[`min`,`max`],Bi=/* @__PURE__ */ new Set([M.Number,M.Currency,M.Progress,M.Rating]),Vi=/* @__PURE__ */ new Set([M.MultiSelect,M.Person,M.Group]),Hi=`General`;function Ui(e){return typeof e==`string`&&Ii.includes(e)}function Wi(e){return!e||typeof e!=`object`?{}:Object.fromEntries(Object.entries(e).filter(e=>Ui(e[1])))}function Gi(e){return Bi.has(e.type)?[...Li,...Ri]:e.type===M.Formula?[...Li,...oa(e)===`date`?zi:Ri]:Li}function Ki(e,t){return Gi(e).includes(t)}function qi(e,t,n){if(!Ki(e,n))return null;let r=t.filter(t=>!Yi(e,t));switch(n){case`count`:return t.length;case`filled`:return r.length;case`empty`:return t.length-r.length;case`unique`:return new Set(r.map(t=>$i(e,t))).size;case`sum`:{let t=Xi(e,r);return t.length?Qi(t):null}case`average`:{let t=Xi(e,r);return t.length?Qi(t)/t.length:null}case`min`:{let t=Xi(e,r);return t.length?Math.min(...t):null}case`max`:{let t=Xi(e,r);return t.length?Math.max(...t):null}}}function Ji(e,t,n){if(n==null||!Number.isFinite(n))return`-`;if(Li.includes(t))return String(n);switch(e.type){case M.Number:return ji(n,ra(e));case M.Currency:return ji(n,ia(e));case M.Progress:return be(n,e.config);case M.Formula:return aa(n,e);default:return ca(n)}}function Yi(e,t){if(t==null)return!0;if(typeof t==`string`)return t.trim()===``;if(Array.isArray(t))return e.type===M.Attachment?!t.some(na):t.length===0;if(e.type===M.Link&&typeof t==`object`){let e=t;return[e.text,e.url].every(e=>e==null||String(e).trim()===``)}return!1}function Xi(e,t){let n=[];for(let r of t){let t=Zi(e,r);t!=null&&n.push(t)}return n}function Zi(e,t){if(typeof t==`number`)return Number.isFinite(t)?t:null;if(e.type!==M.Formula&&typeof t==`string`&&t.trim()!==``){let e=Number(t);return Number.isFinite(e)?e:null}return null}function Qi(e){let t=0,n=0;for(let r of e){let e=t+r;n+=Math.abs(t)>=Math.abs(r)?t-e+r:r-e+t,t=e}return t+n}function $i(e,t){return Vi.has(e.type)&&Array.isArray(t)?ta([...new Set(t.map(e=>ta(ea(e))))].sort()):ta(ea(t))}function ea(e){return Array.isArray(e)?e.map(ea):e&&typeof e==`object`?Object.fromEntries(Object.entries(e).sort(([e],[t])=>e.localeCompare(t)).map(([e,t])=>[e,ea(t)])):e}function ta(e){return JSON.stringify(e)??`undefined`}function na(e){if(typeof e==`string`)return e.trim()!==``;if(!e||typeof e!=`object`)return!1;let t=e;return[t.id,t.name,t.source,t.url,t.thumbnail].some(e=>e!=null&&String(e).trim()!==``)}function ra(e){let t=la(e.config);return{decimalPlaces:da(t.decimalPlaces,0,20,2),separatorStyle:typeof t.separatorStyle==`string`?t.separatorStyle:`local`,locale:typeof t.locale==`string`?t.locale:void 0,useThousands:t.useThousands!==!1,abbreviation:typeof t.abbreviation==`string`?t.abbreviation:`none`}}function ia(e){let t=la(e.config);return{...ra(e),currencySymbol:String(t.currencySymbol??`$`)}}function aa(e,t){let n=sa(t);if(n===Hi)return ca(e);try{return String(ht.format(n,e,{throws:!1}))}catch{return ca(e)}}function oa(e){let t=la(la(e.config).numfmt);return typeof t.type==`string`?t.type:`general`}function sa(e){let t=la(la(e.config).numfmt);return String(t.pattern??Hi).trim()||Hi}function ca(e){return String(Number.isInteger(e)?e:Math.round((e+2**-52)*100)/100)}function la(e){return e&&typeof e==`object`&&!Array.isArray(e)?e:{}}function ua(e){if(e==null||e===``)return null;let t=typeof e==`number`?e:Number(e);return Number.isFinite(t)?t:null}function da(e,t,n,r){let i=ua(e);return i==null?r:Math.max(t,Math.min(n,Math.round(i)))}function fa(e,t){let n=B.deepClone(e),r=t.includeRecords===!0,i=/* @__PURE__ */ new Map,a=t.createViewId??(()=>`view-${lt(8)}`),o=n.viewOrder;return t.regenerateViewIds&&(o=n.viewOrder.map(e=>{let t=a();return i.set(e,t),t})),{...n,id:t.id,name:t.name,records:r?n.records:{},recordOrder:r?n.recordOrder:[],rowIndex:r?n.rowIndex:{},rowId:r?n.rowId:{},cellData:r?n.cellData:{},resources:r?n.resources:{},viewOrder:o,views:Object.fromEntries(Object.entries(n.views??{}).map(([e,n])=>{let r=i.get(e)??(t.regenerateViewIds?a():e);return[r,{...n,id:r,tableId:t.id}]}))}}function pa(e){return new k().getAffectedRanges(e)}function ma(e){return e.patches.map(t=>({unitId:e.unitId,sheetId:e.tableId,range:{startRow:t.row,endRow:t.row,startColumn:t.col,endColumn:t.col}}))}function ha(e,t){return{[e]:Object.fromEntries(t.map(e=>[e,`1`]))}}let ga=class extends Ge{constructor(e,t){super(),this._activeDirtyManagerService=e,this._univerInstanceService=t,this._initialize()}_initialize(){this._activeDirtyManagerService.register(A.id,{commandId:A.id,getDirtyData:e=>this._getJson1DirtyData(e.params)}),this.disposeWithMe(_t(()=>{this._activeDirtyManagerService.remove(A.id)}))}_getTable(e,t){let n=this._univerInstanceService.getUnit(e,V.UNIVER_BASE);return(n==null?void 0:n.getSnapshot().tables[t])??null}_getJson1DirtyData(e){if(e.trigger===`formula.mutation.set-formula-calculation-result`)return{};if(!e.unitId||!e.op)return{clearDependencyTreeCache:this._getAllBaseDependencyTreeCacheMap(),forceCalculation:!0};let t=pa(e.op),n=this._getStructureDirtyTableIds(e.unitId,t);if(n.length)return{clearDependencyTreeCache:ha(e.unitId,n),forceCalculation:!0};let r=Object.entries(this._groupCellInvalidations(e.unitId,t)).flatMap(([t,n])=>ma({unitId:e.unitId,tableId:t,patches:n}));return r.length?{dirtyRanges:r}:{}}_getStructureDirtyTableIds(e,t){let n=t.filter(e=>e.reason!==`cell`).flatMap(t=>t.tableId?[t.tableId]:this._getAllTableIds(e));return Array.from(new Set(n))}_groupCellInvalidations(e,t){let n=/* @__PURE__ */ new Set;return t.reduce((t,r)=>{let i=this._toDirtyRangePatch(e,r);if(!i)return t;let a=`${r.tableId}:${i.row}:${i.col}`;return n.has(a)||(n.add(a),t[r.tableId]=t[r.tableId]??[],t[r.tableId].push(i)),t},{})}_toDirtyRangePatch(e,t){if(t.reason!==`cell`)return null;if(t.row!=null&&t.column!=null)return{row:t.row,col:t.column,value:null};if(!t.recordId||!t.fieldId)return null;let n=this._getTable(e,t.tableId);if(!n)return null;let r=f(n,t.recordId,t.fieldId);return r?{row:r.row,col:r.col,value:null}:null}_getAllTableIds(e){let t=this._univerInstanceService.getUnit(e,V.UNIVER_BASE);return Object.values((t==null?void 0:t.getSnapshot().tables)??{}).map(e=>e.id)}_getAllBaseDependencyTreeCacheMap(){let e=this._univerInstanceService.getAllUnitsForType(V.UNIVER_BASE).map(e=>ha(e.getUnitId(),Object.keys(e.getSnapshot().tables)));return Object.assign({},...e)}};ga=O([E(0,bt),E(1,L)],ga);let _a=class extends Ge{constructor(e,t,n,r){super(),this._commandService=e,this._univerInstanceService=t,this._formulaCalculationTriggerService=n,this._formulaCalculationSessionService=r,D(this,`_formulaService`,new Mn),D(this,`_registeredSuperTableNames`,/* @__PURE__ */ new Map),D(this,`_scheduledSuperTableSyncs`,/* @__PURE__ */ new Set),D(this,`_formulaSyncQueue`,Promise.resolve()),D(this,`_applyingFormulaResults`,!1),this._initialize()}_initialize(){this._univerInstanceService.getAllUnitsForType(V.UNIVER_BASE).forEach(e=>{this._scheduleSuperTableSync(e)}),this.disposeWithMe(this._univerInstanceService.getTypeOfUnitAdded$(V.UNIVER_BASE).subscribe(({unit:e})=>{this._scheduleSuperTableSync(e)})),this.disposeWithMe(this._univerInstanceService.getTypeOfUnitDisposed$(V.UNIVER_BASE).subscribe(e=>{this._enqueueFormulaSync(()=>this._removeSuperTables(e.getUnitId()))})),this.disposeWithMe(this._commandService.onCommandExecuted(e=>this._onCommandExecuted(e)))}_onCommandExecuted(e){if(e.id===St.id){ya(e.params)&&this._applyFormulaResult(e.params);return}if(this._applyingFormulaResults||e.id!==A.id)return;let t=va(e.params)?e.params:void 0,n=t!=null&&t.unitId?[this._univerInstanceService.getUnit(t.unitId,V.UNIVER_BASE)].filter(e=>!!e):this._univerInstanceService.getAllUnitsForType(V.UNIVER_BASE),r=t==null?void 0:t.op;n.forEach(e=>{let t=r?pa(r):[];(!r||t.some(e=>e.reason!==`cell`))&&this._scheduleSuperTableSync(e)})}_scheduleSuperTableSync(e){let t=e.getUnitId();this._scheduledSuperTableSyncs.has(t)||(this._scheduledSuperTableSyncs.add(t),this._enqueueFormulaSync(async()=>{this._scheduledSuperTableSyncs.delete(t),await this._syncSuperTables(e)}))}async _syncSuperTables(e){let t=e.getUnitId(),n=this._formulaService.getSuperTableRegistrations(e.getSnapshot()),r=new Set(n.map(({tableName:e})=>e)),i=this._registeredSuperTableNames.get(t)??/* @__PURE__ */ new Set;for(let e of i)if(!r.has(e)&&!await this._executeSuperTableMutationWhileActive(xt.id,{unitId:t,tableName:e}))return;for(let{tableName:e,reference:r}of n)if(!await this._executeSuperTableMutationWhileActive(Ct.id,{unitId:t,tableName:e,reference:r}))return;this._registeredSuperTableNames.set(t,r)}async _removeSuperTables(e){let t=this._registeredSuperTableNames.get(e)??/* @__PURE__ */ new Set;for(let n of t)if(!await this._executeSuperTableMutationWhileActive(xt.id,{unitId:e,tableName:n}))return;this._registeredSuperTableNames.delete(e)}async _executeSuperTableMutationWhileActive(e,t){return!this._disposed&&await this._commandService.executeCommand(e,t,{onlyLocal:!0})&&!this._disposed}_enqueueFormulaSync(e){this._formulaSyncQueue=this._formulaSyncQueue.then(e,e).catch(()=>void 0)}_applyFormulaResult(e){let t=this._univerInstanceService.getAllUnitsForType(V.UNIVER_BASE);if(!t.some(t=>{var n;return((n=e.unitData)==null?void 0:n[t.getUnitId()])!=null}))return;let n=t.flatMap(t=>Nn(t.getSnapshot(),e));if(!n.length){this._formulaCalculationSessionService.markResultApplied(At.BASE,e);return}let r=new K,i=n.flatMap(e=>ma({unitId:e.unitId,tableId:e.tableId,patches:e.patches}));this._applyingFormulaResults=!0,(async()=>{for(let e of n){let t=this._univerInstanceService.getUnit(e.unitId,V.UNIVER_BASE);t&&await this._commandService.executeCommand(A.id,{unitId:e.unitId,op:r.setRangeValues(t.getSnapshot(),e.tableId,e.patches),trigger:St.id},{onlyLocal:!0})}await this._commandService.executeCommand(wt.id,{dirtyRanges:i},{onlyLocal:!0})})().finally(()=>{this._applyingFormulaResults=!1,this._formulaCalculationSessionService.markResultApplied(At.BASE,e)})}};_a=O([E(0,F),E(1,L),E(2,Xe(yt)),E(3,Xe(kt))],_a);function va(e){return typeof e==`object`&&!!e&&`unitId`in e&&typeof e.unitId==`string`&&`op`in e&&Array.isArray(e.op)}function ya(e){return typeof e==`object`&&!!e&&`unitData`in e&&`unitOtherData`in e}const ba=/* @__PURE__ */ new Set([Zr.id,Qr.id,bi.id]);let xa=class extends Ge{constructor(e,t,n,r){super(),this._injector=e,this._commandService=t,this._permissionService=n,this._instanceService=r,this._instanceService.getAllUnitsForType(V.UNIVER_BASE).forEach(e=>this._registerUnitPermissionPoints(e.getUnitId())),this.disposeWithMe(this._instanceService.getTypeOfUnitAdded$(V.UNIVER_BASE).subscribe(({unit:e})=>this._registerUnitPermissionPoints(e.getUnitId()))),this.disposeWithMe(this._commandService.beforeCommandExecuted((e,t)=>this._check(e,t))),this.disposeWithMe(this._instanceService.getTypeOfUnitDisposed$(V.UNIVER_BASE).subscribe(e=>{this._injector.get(tt).clearUnit(e.getUnitId()),fn(this._permissionService,e.getUnitId())}))}_registerUnitPermissionPoints(e){Qt.forEach(t=>{let n=cn(e,e,t);this._permissionService.getPermissionPoint(n.id)||this._permissionService.addPermissionPoint(n)}),this._injector.get(tt).initializeUnit({unitId:e,objectId:e,objectType:W.Base})}_check(e,t){var n;if(t!=null&&t.fromCollab||t!=null&&t.fromChangeset)return;let r=Na(e.params)?e.params:void 0;if(t!=null&&t.onlyLocal&&e.id===`base.mutation.apply-base-json1`&&(r==null?void 0:r.trigger)===St.id)return;let i=Ca(e.id),a=Sa(r==null?void 0:r.unitId,t)??(i?(n=this._instanceService.getCurrentUnitOfType(V.UNIVER_BASE))==null?void 0:n.getUnitId():void 0);if(!a||this._instanceService.getUnitType(a)!==V.UNIVER_BASE)return;if(i){if(!ln(this._permissionService,a,a,i))throw this._injector.get(Ke).emitPermissionDenied(a),new Ue(`Base ${U[i]} permission denied.`);return}if(ba.has(e.id)||!e.id.startsWith(`base.command.`)&&!e.id.startsWith(`base.mutation.`))return;let o=this._instanceService.getUnit(a,V.UNIVER_BASE),s=o?wa(o,r??{},e.id):[];if(!o||!pn(this._permissionService,a,s))throw this._injector.get(Ke).emitPermissionDenied(a,s),new Ue(`Base edit permission denied.`)}};xa=O([E(0,Xe(Ze)),E(1,F),E(2,Je),E(3,L)],xa);function Sa(e,t){return typeof e==`string`?e:typeof(t==null?void 0:t.unitId)==`string`?t.unitId:void 0}function Ca(e){if(e===`bases-exchange-client.operation.export-base`||e===`bases-exchange-client.operation.export-base-by-format`)return U.Export;if(e.startsWith(`thread-comment.command.`)||e.startsWith(`thread-comment.mutation.`))return U.Comment}function wa(e,t,n){if(n===`base.mutation.apply-base-json1`&&Ma(t.op))return Ta(e,t.op);let r=/* @__PURE__ */ new Set,i=ka(t,`table`),a=typeof t.tableId==`string`?t.tableId:i[0];if(i.forEach(e=>r.add(q(e))),!a)return[...r];let o=e.getSnapshot().tables[a],s=new Set(ka(t,`field`)),c=new Set(ka(t,`record`)),l=new Set(ka(t,`view`));return Aa(o,t.patches,c,s),ja(t,s),s.forEach(e=>r.add(J(a,e))),c.forEach(e=>r.add(Y(a,e))),l.forEach(e=>r.add(on(a,e))),[...r]}function Ta(e,t){let n=e.getSnapshot(),r=new k().getAffectedRanges(t),i=Ea(t),a=/* @__PURE__ */ new Set;return r.forEach(e=>{var t,r;let{tableId:o}=e;if(!o)return;a.add(q(o));let s=n.tables[o],c=e.recordId??(e.row==null||s==null||(t=s.rowId)==null?void 0:t[e.row]),l=e.fieldId??(e.column==null||s==null||(r=s.colId)==null?void 0:r[e.column]);c&&a.add(Y(o,c)),l&&a.add(J(o,l)),e.viewId&&a.add(on(o,e.viewId)),(e.reason===`table`||i.has(o))&&s&&(Object.keys(s.fields).forEach(e=>a.add(J(o,e))),Object.keys(s.records).forEach(e=>a.add(Y(o,e))),Object.keys(s.views).forEach(e=>a.add(on(o,e)))),e.reason===`record`&&s&&Object.keys(s.fields).forEach(e=>a.add(J(o,e)))}),r.length===0&&Oa(t,`tableOrder`)&&Object.keys(n.tables).forEach(e=>a.add(q(e))),[...a]}function Ea(e){let t=/* @__PURE__ */ new Set,n=e=>{Array.isArray(e)&&e.forEach((r,i)=>{r===`tables`&&typeof e[i+1]==`string`&&Da(e[i+2])&&t.add(e[i+1]),n(r)})};return n(e),t}function Da(e){return!!e&&typeof e==`object`&&!Array.isArray(e)&&(`i`in e||`r`in e||`p`in e||`d`in e||`es`in e||`ena`in e||`et`in e)}function Oa(e,t){return Array.isArray(e)&&e.some(e=>e===t||Oa(e,t))}function ka(e,t){let n=/* @__PURE__ */ new Set,r=`${t}Id`,i=`${t}Ids`,a=(e,o=``)=>{typeof e==`string`?o===r&&n.add(e):Array.isArray(e)?o===i?e.forEach(e=>{typeof e==`string`&&n.add(e)}):e.forEach(e=>a(e,o)):Na(e)&&Object.entries(e).forEach(([e,r])=>{e===`id`&&o===t&&typeof r==`string`?n.add(r):a(r,e)})};return a(e),[...n]}function Aa(e,t,n,r){e&&Array.isArray(t)&&t.forEach(t=>{if(!Na(t))return;let i=t;if(typeof i.row==`number`){var a;let t=(a=e.rowId)==null?void 0:a[i.row];t&&n.add(t)}if(typeof i.col==`number`){var o;let t=(o=e.colId)==null?void 0:o[i.col];t&&r.add(t)}})}function ja(e,t,n=``){e&&typeof e==`object`&&(Array.isArray(e)?e.forEach(e=>ja(e,t,n)):Na(e)&&Object.entries(e).forEach(([e,r])=>{n===`values`&&t.add(e),ja(r,t,e)}))}function Ma(e){return Array.isArray(e)}function Na(e){return typeof e==`object`&&!!e&&!Array.isArray(e)}const Pa=ct(`base.resource.service`);let Fa=class{constructor(e){this._univerInstanceService=e}getBase(e){return this._univerInstanceService.getUnit(e,V.UNIVER_BASE)??null}getAllBases(){return this._univerInstanceService.getAllUnitsForType(V.UNIVER_BASE)}createBase(e){return this._univerInstanceService.createUnit(V.UNIVER_BASE,e)}};Fa=O([E(0,L)],Fa);const Ia=ct(`base.view-registry.service`);var La=class extends Ge{constructor(...e){super(...e),D(this,`_plugins`,/* @__PURE__ */ new Map)}register(e){return this._plugins.set(e.type,e),_t(()=>this._plugins.delete(e.type))}get(e){let t=this._plugins.get(e);if(!t)throw Error(`[BaseViewRegistryService]: view type "${e}" is not registered.`);return t}getAll(){return Array.from(this._plugins.values())}};const Ra=[A];let za=class extends rt{constructor(e=Ce,t,n,r,i,a){super(),this._config=e,this._injector=t,this._commandService=n,this._univerInstanceService=r,this._configService=i,this._dataSyncPrimaryController=a,D(this,`_baseSyncDisposables`,/* @__PURE__ */ new Map);let{...o}=pt({},Ce,this._config);this._configService.setConfig(Ee,o),this._univerInstanceService.registerCtorForType(V.UNIVER_BASE,Re),this._initDependencies(),this._initCommands(),this._initRegistries(),this._initDataSync()}onStarting(){this._injector.add([xi]),this.disposeWithMe(this._injector.get(F).registerCommand(Si)),this.disposeWithMe(this._injector.get(F).registerCommand(Ci)),this.disposeWithMe(this._injector.get(tt).registerRuleModel(W.Base,this._injector.get(xi),Si.id,Ci.id))}onReady(){vt(this._injector,[[Fa],[bn],[ga]]),this._univerInstanceService.getAllUnitsForType(V.UNIVER_BASE).forEach(e=>this._syncBaseUnit(e.getUnitId())),vt(this._injector,[[_a],[xa]])}_initDependencies(){[[Ne,{useClass:Ae}],[Ia,{useClass:La}],[Oe,{useClass:pe}],[Pa,{useClass:Fa}],[mn,{useClass:hn}],[X,{useClass:bn}],[ga],[_a],[xa]].forEach(e=>this._injector.add(e))}_initCommands(){[gr,qr,Pr,ci,nr,rr,ir,ei,fr,ur,sr,Nr,cr,Lr,fi,Hr,dr,Ir,ui,li,lr,Mr,Vr,bi,jr,Rr,Jr,Xr,Fr,Kr,yi,mi,Yr,Zr,Qr,di,ai,si,oi,ti,ri,ii,ni,...Ra].forEach(e=>this.disposeWithMe(this._commandService.registerCommand(e))),Ra.forEach(e=>{var t;return(t=this._dataSyncPrimaryController)==null?void 0:t.registerSyncingMutations(e)})}_initDataSync(){this._dataSyncPrimaryController&&(this.disposeWithMe(this._univerInstanceService.getTypeOfUnitAdded$(V.UNIVER_BASE).subscribe(({unit:e})=>{this._syncBaseUnit(e.getUnitId())})),this.disposeWithMe(this._univerInstanceService.getTypeOfUnitDisposed$(V.UNIVER_BASE).subscribe(e=>{var t;(t=this._baseSyncDisposables.get(e.getUnitId()))==null||t.dispose(),this._baseSyncDisposables.delete(e.getUnitId())})))}_syncBaseUnit(e){this._dataSyncPrimaryController&&!this._baseSyncDisposables.has(e)&&this._baseSyncDisposables.set(e,this._dataSyncPrimaryController.syncUnit(e))}_initRegistries(){let e=this._injector.get(Ne);Me().forEach(t=>e.register(t));let t=this._injector.get(Ia),n=this._injector.get(Oe);ce().forEach(e=>{t.register(e),n.registerView(e)})}};D(za,`pluginName`,`BASES_PLUGIN`),D(za,`packageName`,te),D(za,`version`,ve),D(za,`type`,V.UNIVER_BASE),za=O([We(Dt,Tt),E(1,Xe(Ze)),E(2,F),E(3,L),E(4,qe),E(5,nt(Ot))],za);function Ba(e){let t=$a(e.query);if(!t)return{query:``,total:0,matches:[]};let n=[],r=[];for(let t=0;t<e.fieldIds.length;t++){if(!(t in e.fieldIds))continue;let n=e.table.fields[e.fieldIds[t]];n&&r.push({field:n,fieldIndex:t})}e.rows.forEach((e,i)=>{r.forEach(({field:r,fieldIndex:a})=>{let o=Va(e.values[r.id],r);if(!o)return;let s=Qa(t,o.searchTexts);s<=0||n.push({recordId:e.recordId,fieldId:r.id,fieldType:r.type,text:o.displayText,score:s,rowIndex:i,fieldIndex:a})})}),n.sort((e,t)=>t.score-e.score||e.rowIndex-t.rowIndex||e.fieldIndex-t.fieldIndex);let i=e.limit==null?n:n.slice(0,Math.max(0,e.limit));return{query:t,total:n.length,matches:to(i)}}function Va(e,t){let n=[];for(let r of Ua(e,t)){let e=$a(r);e&&n.push(e)}return n.length?{displayText:Ha(e,t),searchTexts:eo(n)}:null}function Ha(e,t){switch(t.type){case M.SingleSelect:case M.MultiSelect:case M.Group:return Wa(e,t).join(`, `);case M.Link:return qa(e);case M.Date:case M.CreatedAt:case M.UpdatedAt:return Ja(e,t);case M.Number:case M.Currency:return Ya(e,t);case M.Progress:return Xa(e,t);case M.Checkbox:return e?`Checked`:`Unchecked`;case M.Attachment:return Za(e).join(`, `);default:return Q(e)}}function Ua(e,t){switch(t.type){case M.SingleSelect:case M.MultiSelect:case M.Group:{let n=Wa(e,t);if(Array.isArray(e)){let t=[...n];for(let n=0;n<e.length;n++)n in e&&t.push(String(e[n]));return t}return e==null?n:[...n,String(e)]}case M.Link:{let t=Ka(e);return[t.text,t.url]}case M.Date:case M.CreatedAt:case M.UpdatedAt:return[Ja(e,t),Q(e)];case M.Number:case M.Currency:return[Ya(e,t),Q(e)];case M.Progress:return[Xa(e,t),Q(e)];case M.Checkbox:return e?[`Checked`,`Yes`,`True`]:[`Unchecked`,`No`,`False`];case M.Attachment:return Za(e);default:return[Q(e)]}}function Wa(e,t){var n;let r=Array.isArray((n=t.config)==null?void 0:n.options)?t.config.options:[],i=[];if(Array.isArray(e)){for(let t=0;t<e.length;t++){if(!(t in e))continue;let n=String(e[t]);n&&i.push(Ga(r,n))}return i}if(e!=null){let t=String(e);t&&i.push(Ga(r,t))}return i}function Ga(e,t){for(let n of e){if(typeof n==`string`&&n===t)return n;if(n&&typeof n==`object`){let e=n;if(String(e.id??e.name??``)===t||String(e.name??``)===t)return String(e.name??e.id??t)}}return t}function Ka(e){if(e&&typeof e==`object`&&!Array.isArray(e)){let t=e;return{text:String(t.text??t.url??``),url:String(t.url??``)}}return{text:Q(e),url:Q(e)}}function qa(e){let t=Ka(e);return t.text||t.url}function Ja(e,t){return typeof e!=`number`||!Number.isFinite(e)?``:i(e,t.config)}function Ya(e,t){let n=typeof e==`number`?e:Number(e);return Number.isFinite(n)?ji(n,t.config):``}function Xa(e,t){let n=typeof e==`number`?e:Number(e);return Number.isFinite(n)?be(n,t.config):``}function Za(e){return Array.isArray(e)?e.flatMap(e=>{if(!e||typeof e!=`object`)return[];let t=e,n=String(t.name??t.url??``).trim();return n?[n]:[]}):[]}function Q(e){if(e==null)return``;if(typeof e==`boolean`)return e?`true`:`false`;if(typeof e==`number`)return String(e);if(Array.isArray(e)){let t=[];for(let n=0;n<e.length;n++){if(!(n in e))continue;let r=Q(e[n]);r&&t.push(r)}return t.join(`, `)}if(typeof e==`object`){let t=e;return String(t.title??t.name??t.text??t.url??``).trim()}return String(e)}function Qa(e,t){let n=0;for(let r of t)r&&(r===e?n=Math.max(n,300):r.startsWith(e)?n=Math.max(n,250):r.includes(e)&&(n=Math.max(n,200)));return n}function $a(e){return e.trim().replace(/\s+/g,` `).toLowerCase()}function eo(e){return Array.from(new Set(e))}function to(e){let t=[];for(let{rowIndex:n,fieldIndex:r,...i}of e)t.push(i);return t}function no(e){return{compress:!0,kind:`base-range-values-v1`,u:e.unitId,t:e.tableId,p:e.patches.map(e=>[e.row,e.col,oo(e.value)]),...e.trigger===void 0?{}:{g:e.trigger}}}function ro(e){if(mo(e,`base-range-values-v1`),!io(e)){if(!po(e))throw Error(`[BaseCompression]: invalid uncompressed range values payload.`);return e}return{unitId:e.u,tableId:e.t,patches:e.p.map(([e,t,n])=>({row:e,col:t,value:so(n)})),...e.g===void 0?{}:{trigger:e.g}}}function io(e){return!$(e)||e.compress!==!0||e.kind!==`base-range-values-v1`?!1:typeof e.u==`string`&&typeof e.t==`string`&&(e.g===void 0||typeof e.g==`string`)&&Array.isArray(e.p)&&e.p.every(e=>Array.isArray(e)&&e.length===3&&Number.isInteger(e[0])&&Number.isInteger(e[1]))}function ao(e){return!$(e)||e.compress!==!0||e.kind!==`base-snapshot-v1`||!$(e.tables)?!1:Object.values(e.tables).every(e=>$(e)&&(e.cd===void 0||Array.isArray(e.cd)&&e.cd.every(e=>Array.isArray(e)&&e.length===3&&Number.isInteger(e[0])&&Number.isInteger(e[1])&&fo(e[2]))))}function oo(e){return uo(e)?co(e):e}function so(e){return fo(e)?lo(e):e}function co(e){return{c:1,...e.v===void 0?{}:{v:e.v},...e.t!==void 0&&e.t!==null?{t:e.t}:e.t===null?{t:null}:{},...e.f!==void 0&&e.f!==null?{f:e.f}:e.f===null?{f:null}:{},...e.si!==void 0&&e.si!==null?{si:e.si}:e.si===null?{si:null}:{},...e.p!==void 0&&e.p!==null?{p:e.p}:e.p===null?{p:null}:{}}}function lo(e){return{...e.v===void 0?{}:{v:e.v},...e.t===void 0?{}:{t:e.t},...e.f===void 0?{}:{f:e.f},...e.si===void 0?{}:{si:e.si},...e.p===void 0?{}:{p:e.p}}}function uo(e){return!!e&&typeof e==`object`&&!Array.isArray(e)&&(Object.prototype.hasOwnProperty.call(e,`v`)||Object.prototype.hasOwnProperty.call(e,`t`)||Object.prototype.hasOwnProperty.call(e,`p`)||Object.prototype.hasOwnProperty.call(e,`f`)||Object.prototype.hasOwnProperty.call(e,`si`))}function fo(e){return $(e)&&e.c===1}function po(e){return $(e)&&typeof e.unitId==`string`&&typeof e.tableId==`string`&&(e.trigger===void 0||typeof e.trigger==`string`)&&Array.isArray(e.patches)&&e.patches.every(e=>$(e)&&Number.isInteger(e.row)&&Number.isInteger(e.col)&&`value`in e)}function mo(e,t){if($(e)&&e.compress===!0){if(e.kind!==t)throw Error(`[BaseCompression]: unsupported compressed payload kind "${String(e.kind)}".`);if(!(t===`base-range-values-v1`?io(e):ao(e)))throw Error(`[BaseCompression]: invalid compressed payload "${t}".`)}}function $(e){return!!e&&typeof e==`object`&&!Array.isArray(e)}const ho=ct(`bases.sync-status.service`);export{rr as AddGanttPredecessorCommand,A as ApplyBaseJson1Mutation,Ii as BASE_FIELD_STAT_TYPES,x as BASE_HIERARCHY_MAX_LEVELS,C as BASE_HIERARCHY_PARENT_FIELD_ID,pr as BASE_TABLE_NAME_REQUIREMENTS,Qt as BASE_UNIT_PERMISSION_ACTIONS,pi as BaseConditionalColorRuleUpdateType,s as BaseEventSource,Mn as BaseFormulaService,d as BaseHierarchyError,Se as BaseHierarchyErrorCode,re as BaseHistoryAction,k as BaseJson1OpApplier,K as BaseJson1OpFactory,sr as BatchCreateBaseRecordsCommand,cr as BatchDeleteBaseRecordsCommand,lr as ChangeBaseFieldTypeCommand,ur as CreateBaseChildRecordCommand,dr as CreateBaseFieldCommand,fr as CreateBaseRecordCommand,gr as CreateBaseTableCommand,jr as CreateBaseViewCommand,Mr as DeleteBaseFieldCommand,Nr as DeleteBaseRecordCommand,Pr as DeleteBaseTableCommand,Fr as DeleteBaseViewCommand,Ir as DuplicateBaseFieldCommand,Lr as DuplicateBaseRecordCommand,Ti as EMBED_BASES_TABLE_RESOURCE_KEY,Rr as EnsureBaseTemporalViewDateFieldsCommand,X as IBaseCommandService,mn as IBasePermissionService,Oe as IBaseProjectionService,ho as IBaseSyncStatusService,Ia as IBaseViewRegistryService,Vr as MoveBaseFieldCommand,Hr as MoveBaseHierarchyRecordCommand,Kr as MoveBaseViewCommand,ir as RemoveGanttPredecessorCommand,qr as RenameBaseTableCommand,Jr as RenameBaseViewCommand,Yr as SetBaseKanbanGroupFieldCommand,Xr as SetBaseNameCommand,Zr as SetBasePermissionCommand,Si as SetBasePermissionRuleMutation,Ci as SetBasePermissionRulesMutation,Qr as SetBasePermissionsCommand,ei as SetBaseRangeValuesCommand,nr as SetBaseRecordValuesCommand,ti as SetBaseTableHierarchyFieldCommand,ni as SetBaseViewFieldOrderCommand,ri as SetBaseViewFieldVisibleCommand,ii as SetBaseViewFieldWidthCommand,ai as SetBaseViewFilterCommand,oi as SetBaseViewGroupCommand,si as SetBaseViewSortCommand,za as UniverBasesPlugin,ci as UpdateBaseCellCommand,ui as UpdateBaseFieldCommand,li as UpdateBaseFieldDescriptionCommand,di as UpdateBaseKanbanConfigCommand,fi as UpdateBaseRecordOrderCommand,mi as UpdateBaseViewConditionalColorRulesCommand,yi as UpdateBaseViewConfigCommand,bi as ValidateBaseFormulaCommand,b as baseDateSerialToDateMs,u as buildBaseHierarchyIndex,qi as calculateBaseFieldStat,pn as canEditBaseTargets,no as compressBaseRangeValuesPayload,fa as copyBaseTableSnapshot,ee as createBaseHierarchyParentField,wi as createDefaultBaseTableSnapshot,Ei as createEmbedBasesTable,Di as createEmbedBasesTableAddMutation,Oi as createEmbedBasesTableRemoveMutation,hr as createUniqueBaseTableName,a as dateToExcelSerial,ro as decompressBaseRangeValuesPayload,m as ensureBaseTableCellLayout,ie as findRecordLinkReferences,i as formatBaseDateValue,Ji as formatBaseFieldStatValue,ji as formatBaseNumberValue,be as formatBaseProgressValue,w as getActiveBaseHierarchyFieldIds,l as getBaseCellFormulaValue,v as getBaseCellValue,y as getBaseConditionalColorOperators,sn as getBaseDashboardPermissionObjectId,J as getBaseFieldPermissionObjectId,ut as getBaseFormulaTableName,T as getBaseHierarchyDeletePromotions,_ as getBaseHierarchyFieldId,ln as getBasePermissionValue,he as getBaseProgressRange,Y as getBaseRecordPermissionObjectId,q as getBaseTablePermissionObjectId,on as getBaseViewPermissionObjectId,ae as getGanttWorkingDayVariance,me as getRecordLinkFieldConfig,Gi as getSupportedBaseFieldStatTypes,e as getVisibleBaseHierarchyRows,ye as hasGanttPredecessorPath,Fi as inferBaseSeparatorStyle,r as isBaseConditionalColorDateField,Yi as isBaseFieldStatEmptyValue,Ki as isBaseFieldStatSupported,Ui as isBaseFieldStatType,se as isBaseHierarchyField,io as isCompressedBaseRangeValuesPayload,Te as isGanttPredecessorField,je as matchesBaseCondition,h as normalizeBaseDateFieldConfig,fe as normalizeBaseDateSerial,Wi as normalizeBaseFieldStatsConfig,ue as normalizeBaseProgressDecimalPlaces,ke as parseRecordLinkIds,De as projectBaseHierarchy,t as resolveBaseDateFieldConfig,_e as resolveBaseHierarchyFieldId,$r as resolveBaseRangeHierarchyPatches,Ba as searchBaseTable,oe as serializeRecordLinkIds,un as setBasePermissionValue,c as traceBasePerformance,_r as uniqueFieldName,we as validateBaseHierarchyMove,de as validateBaseHierarchyRecordCreations,ne as validateBaseHierarchyRecordPatches,mr as validateBaseTableName,le as validateCanonicalRecordLinkValue,xe as validateRecordLinkField,ge as validateRecordLinkValue};
|
package/lib/es/worker.js
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
import{
|
|
1
|
+
import{_ as e,a as t,b as n,g as r,i,l as a,n as o,o as s,s as c,u as l,v as u,y as d}from"./config-DtzarxLi.js";import{BaseDataModel as f,DependentOn as p,ICommandService as m,IConfigService as h,IUniverInstanceService as g,Inject as _,Injector as v,Plugin as y,UniverInstanceType as b,merge as x}from"@univerjs/core";import{UniverLicensePlugin as S}from"@univerjs-pro/license";import{UniverProFormulaEnginePlugin as C}from"@univerjs-pro/engine-formula/worker";let w=class extends y{constructor(e=i,t,n,r,s){super(),this._config=e,this._injector=t,this._commandService=n,this._configService=r,this._univerInstanceService=s;let{...p}=x({},i,this._config);this._configService.setConfig(o,p),this._univerInstanceService.registerCtorForType(b.UNIVER_BASE,f),[[d,{useClass:u}],[l,{useClass:a}]].forEach(e=>this._injector.add(e)),this.disposeWithMe(this._commandService.registerCommand(c))}};n(w,`pluginName`,`REMOTE_BASES_PLUGIN`),n(w,`packageName`,t),n(w,`version`,s),n(w,`type`,b.UNIVER_BASE),w=r([p(S,C),e(1,_(v)),e(2,m),e(3,h),e(4,g)],w);export{w as UniverRemoteBasesPlugin};
|