"use strict";(self.webpackChunk_aidoop_aidoop_r=self.webpackChunk_aidoop_aidoop_r||[]).push([[3126],{13126:(e,t,i)=>{i.r(t),i.d(t,{Scenario:()=>k});var a=i(50493),s=(i(68784),i(62392)),r=i(16682),n=i(94430),o=i(56765),c=i(27805),d=i(64710),l=i(53421),p=i(36179);const h=["name","description","sequence","task","connection","params","result","skip","log"];let m=class extends((0,l.NC)(l.z2)(n.oi)){render(){return n.dy`
      <ox-grist
        .mode=${(0,p.s2)()?"LIST":"GRID"}
        .config=${this.gristConfig}
        .fetchHandler=${this.fetchHandler.bind(this)}
      ></ox-grist>
      <div class="button-container">
        <mwc-button raised danger @click=${this._deleteSteps.bind(this)}>${l.z2.t("button.delete")}</mwc-button>
        <mwc-button raised @click=${this._updateSteps.bind(this)}>${l.z2.t("button.save")}</mwc-button>
      </div>
    `}async firstUpdated(){this.gristConfig={list:{fields:["name","description","task"]},columns:[{type:"gutter",gutterName:"row-selector",multiple:!0},{type:"gutter",gutterName:"button",icon:"add",handlers:{click:"record-copy"}},{type:"gutter",gutterName:"sequence"},{type:"gutter",gutterName:"button",icon:"arrow_upward",handlers:{click:"move-up"}},{type:"gutter",gutterName:"button",icon:"arrow_downward",handlers:{click:"move-down"}},{type:"number",name:"sequence",hidden:!0},{type:"string",name:"id",hidden:!0},{type:"string",name:"name",header:l.z2.t("field.name"),record:{editable:!0},width:140},{type:"string",name:"description",header:l.z2.t("field.description"),record:{editable:!0},width:180},{type:"boolean",name:"result",header:l.z2.t("field.result"),record:{editable:!0},width:80},{type:"boolean",name:"skip",header:l.z2.t("field.skip"),record:{editable:!0},width:80},{type:"boolean",name:"log",header:l.z2.t("field.log"),record:{editable:!0},width:80},{type:"connection",name:"connection",header:l.z2.t("field.connection"),record:{editable:!0},width:160},{type:"task-type",name:"task",header:l.z2.t("field.task"),record:{renderer:d.p,editable:!0,help:e=>{var t,i;return null===(i=null===(t=this.taskTypes)||void 0===t?void 0:t[e])||void 0===i?void 0:i.help},connectionName:async(e,t,i,a,s)=>i.connection},width:120},{type:"parameters",name:"params",header:l.z2.t("field.params"),record:{editable:!0,options:(e,t,i,a,s)=>{var r,{name:n,parameterSpec:o,help:c}=i.task?null===(r=this.taskTypes)||void 0===r?void 0:r[i.task]:{};return{name:n,spec:o,help:c,context:this.grist}},renderer:"json5"},width:200}],rows:{selectable:{multiple:!0}},pagination:{infinite:!0},sorters:[{name:"sequence"}]}}async fetchHandler({page:e,limit:t,sorters:i=[]}){const a=await c.Lp.query({query:r.ZP`
        query {
          steps ( 
            filters: { 
              name: "scenarioId",
              value: "${this.scenario.id}",
              operator: "eq" 
            },
            sortings: { name: "sequence" }
          ) {
            items {
              id
              ${h.join("\n")}
            }
            total
          }
        }
      `});return{total:a.data.steps.total||0,records:a.data.steps.items||[]}}async fetchTaskType(e){return(await c.Lp.query({query:r.ZP`
        query ($name: String!) {
          taskType(name: $name) {
            name
            description
            help
            parameterSpec {
              type
              name
              label
              placeholder
              property
            }
          }
        }
      `,variables:{name:e}})).data.taskType}async _updateSteps(){let e=this.grist._data.records;if(e&&e.length){e=e.map((e=>{var t={};const i=e.__dirtyfields__;for(let e in i)t[e]=i[e].after;return Object.assign(Object.assign({},e.__origin__),t)}));(await c.Lp.mutate({mutation:r.ZP`
          mutation ($scenarioId: String!, $patches: [StepPatch!]!) {
            updateMultipleStep(scenarioId: $scenarioId, patches: $patches) {
              name
            }
          }
        `,variables:{scenarioId:this.scenario.id,patches:e}})).errors||(this.grist.fetch(),this.requestRefresh())}}async _deleteSteps(){this.grist.deleteSelectedRecords(!0)}requestRefresh(){this.dispatchEvent(new CustomEvent("requestRefresh"))}};m.styles=[n.iv`
      :host {
        display: flex;
        flex-direction: column;

        background-color: #fff;
      }

      ox-grist {
        flex: 1;
      }

      .button-container {
        display: flex;
        margin-left: auto;
        padding: var(--padding-default);
      }

      form {
        position: relative;
      }
      [danger] {
        --mdc-theme-primary: var(--mdc-danger-button-primary-color);
      }
      mwc-button {
        margin-left: var(--margin-default);
      }
    `],(0,a.gn)([(0,o.Cb)({type:Object}),(0,a.w6)("design:type",Object)],m.prototype,"scenario",void 0),(0,a.gn)([(0,o.Cb)({type:Object}),(0,a.w6)("design:type",Object)],m.prototype,"gristConfig",void 0),(0,a.gn)([(0,o.Cb)({type:Object}),(0,a.w6)("design:type",Object)],m.prototype,"taskTypes",void 0),(0,a.gn)([(0,o.IO)("ox-grist"),(0,a.w6)("design:type",s.N)],m.prototype,"grist",void 0),m=(0,a.gn)([(0,o.Mo)("scenario-detail")],m);var u=i(15633);let g=class extends n.oi{constructor(){super(...arguments),this.columns={list:{fields:["name","description"]},pagination:{infinite:!0},columns:[{type:"string",name:"name",header:l.z2.t("field.name"),width:150},{type:"string",name:"type",header:l.z2.t("field.type"),width:100},{type:"string",name:"description",header:l.z2.t("field.description"),width:200},{type:"crontab",name:"schedule",header:l.z2.t("field.schedule"),width:80},{type:"string",name:"timezone",header:l.z2.t("field.timezone"),width:120}]}}render(){return n.dy`
      <ox-grist
        .mode=${(0,p.s2)()?"LIST":"GRID"}
        .config=${this.columns}
        .data=${{records:this.scenarios}}
      ></ox-grist>

      <div class="button-container">
        <mwc-button raised @click=${this.save.bind(this)}>${l.z2.t("button.save")}</mwc-button>
      </div>
    `}async save(){var e;(null===(e=(await c.Lp.mutate({mutation:r.ZP`
        mutation importScenarios($scenarios: [ScenarioPatch!]!) {
          importScenarios(scenarios: $scenarios)
        }
      `,variables:{scenarios:this.scenarios}})).errors)||void 0===e?void 0:e.length)||this.dispatchEvent(new CustomEvent("imported"))}};g.styles=[u.Pn,n.iv`
      :host {
        display: flex;
        flex-direction: column;

        background-color: #fff;
      }
      ox-grist {
        flex: 1;
      }
      .button-container {
        display: flex;
        margin-left: auto;
        padding: var(--padding-default);
      }
      mwc-button {
        margin-left: var(--margin-default);
      }
    `],(0,a.gn)([(0,o.Cb)({type:Array}),(0,a.w6)("design:type",Object)],g.prototype,"scenarios",void 0),(0,a.gn)([(0,o.Cb)({type:Object}),(0,a.w6)("design:type",Object)],g.prototype,"columns",void 0),g=(0,a.gn)([(0,o.Mo)("scenario-importer")],g);var y=i(50383),f=i.n(y),b=i(28120),v=i(37077),w=i(77675),$=i(51266);const S=Intl.DateTimeFormat().resolvedOptions().timeZone,x=["",S,...f().tz.names().filter((e=>e!==S))];function z(e){return e&&"UNLOADED"!==e}let k=class extends((0,b.$)(w.h)((0,l.NC)(l.z2)(w.B4))){constructor(){super(...arguments),this.active=!1}static get styles(){return[u.Pn,u.Ix,n.iv`
        :host {
          display: flex;
          flex-direction: column;

          overflow: hidden;
        }

        ox-grist {
          overflow-y: auto;
          flex: 1;
        }

        #filters {
          display: flex;
          flex-direction: row;
          justify-content: space-between;

          background-color: white;
        }

        #filters > * {
          padding: var(--padding-default) var(--padding-wide);
        }
      `]}get context(){var e;return{title:l.z2.t("text.scenario list"),search:{handler:e=>{this.grist.searchText=e},value:(null===(e=this.grist)||void 0===e?void 0:e.searchText)||""},help:"integration/ui/scenario",actions:[Object.assign({title:l.z2.t("button.start monitor"),action:()=>{(0,w.c4)("integration-monitor")}},u.jO.preview),Object.assign({title:l.z2.t("button.copy"),action:this._copyScenario.bind(this)},u.jO.copy),Object.assign({title:l.z2.t("button.save"),action:this._updateScenario.bind(this)},u.jO.save),Object.assign({title:l.z2.t("button.delete"),action:this._deleteScenario.bind(this)},u.jO.delete)],exportable:{name:l.z2.t("text.scenario list"),data:this.exportHandler.bind(this)},importable:{handler:this.importHandler.bind(this)}}}render(){return n.dy`
      <ox-grist
        .mode=${(0,p.s2)()?"LIST":"GRID"}
        .config=${this.gristConfig}
        .fetchHandler=${this.fetchHandler.bind(this)}
      >
      </ox-grist>
    `}async pageInitialized(e){this.fetchTaskTypes(),this.gristConfig={list:{fields:["name","description","schedule","active"]},columns:[{type:"gutter",gutterName:"sequence"},{type:"gutter",gutterName:"row-selector",multiple:!0},{type:"gutter",gutterName:"button",title:l.z2.t("title.scenario schedule (un)register"),icon:e=>!e||e.scheduleId?"event_available":"",handlers:{click:(e,t,i,a,s)=>{a&&a.name&&(a.scheduleId?this.stopScenarioSchedule(a):this.startScenarioSchedule(a))}}},{type:"gutter",gutterName:"button",name:"state",icon:e=>e&&z(e.state)?"pause":"play_arrow",handlers:{click:(e,t,i,a,s)=>{a&&a.name&&(z(a.state)?this.stopScenario(a):this.startScenario(a))}}},{type:"gutter",gutterName:"button",icon:"reorder",handlers:{click:(e,t,i,a,s)=>{a.id&&(0,v.Mw)(n.dy`
                  <scenario-detail
                    .scenario=${a}
                    .taskTypes=${this.taskTypes}
                    @requestRefresh="${()=>this.grist.fetch()}"
                  ></scenario-detail>
                `,{backdrop:!0,help:"integration/ui/scenario-detail",size:"large",title:l.z2.t("title.scenario-detail")})}}},{type:"string",name:"name",label:!0,header:l.z2.t("field.name"),record:{editable:!0},filter:"search",sortable:!0,width:300,validation:function(e,t,i,a){return!z(i.state)||((0,v.h4)({level:"warn",message:"scenario name cannot be changed during execution."}),!1)}},{type:"string",name:"type",header:l.z2.t("field.type"),record:{editable:!0},filter:"search",width:100},{type:"string",name:"description",label:!0,header:l.z2.t("field.description"),record:{editable:!0},filter:"search",width:350},{type:"crontab",name:"schedule",label:!0,header:l.z2.t("field.schedule"),record:{editable:!0},width:110},{type:"select",name:"timezone",header:l.z2.t("field.timezone"),record:{editable:!0,options:x},width:120},{type:"checkbox",name:"active",label:!0,header:l.z2.t("field.active"),record:{align:"center",editable:!0},sortable:!0,width:60},{type:"object",name:"updater",header:l.z2.t("field.updater"),record:{editable:!1},sortable:!0,width:85},{type:"datetime",name:"updatedAt",header:l.z2.t("field.updated_at"),record:{editable:!1},sortable:!0,width:180}],rows:{selectable:{multiple:!0}},sorters:[{name:"name"}]}}async pageUpdated(e,t){this.active&&this.grist.fetch()}async fetchHandler({page:e,limit:t,sortings:i=[],filters:a=[]}){const s=await c.Lp.query({query:r.ZP`
        query ($filters: [Filter!], $pagination: Pagination, $sortings: [Sorting!]) {
          responses: scenarios(filters: $filters, pagination: $pagination, sortings: $sortings) {
            items {
              id
              name
              type
              description
              active
              state
              schedule
              scheduleId
              timezone
              updater {
                id
                name
              }
              updatedAt
              steps {
                name
                description
                sequence
                task
                skip
                log
                connection
                params
              }
            }
            total
          }
        }
      `,variables:{filters:a,pagination:{page:e,limit:t},sortings:i}});return{total:s.data.responses.total||0,records:s.data.responses.items||[]}}async fetchTaskTypes(){const e=await c.Lp.query({query:r.ZP`
        query {
          taskTypes {
            items {
              name
              description
              help
              parameterSpec {
                type
                name
                label
                placeholder
                property
              }
            }
          }
        }
      `});e.errors?$.error("fetch taskTypes error"):this.taskTypes=e.data.taskTypes.items.reduce(((e,t)=>(e[t.name]=t,e)),{})}async _deleteScenario(){if(confirm(l.z2.t("text.sure_to_x",{x:l.z2.t("text.delete")}))){const e=this.grist.selected.map((e=>e.id));if(e&&e.length>0){(await c.Lp.mutate({mutation:r.ZP`
            mutation ($ids: [String!]!) {
              deleteScenarios(ids: $ids)
            }
          `,variables:{ids:e}})).errors||(this.grist.fetch(),(0,v.h4)({message:l.z2.t("text.info_x_successfully",{x:l.z2.t("text.delete")})}))}}}async _copyScenario(){var e=this.grist.selected;0!=e.length&&(confirm(l.z2.t("text.sure_to_x",{x:l.z2.t("text.copy")}))&&((await c.Lp.mutate({mutation:r.ZP`
        mutation ($ids: [String!]!) {
          copyScenarios(ids: $ids) {
            id
          }
        }
      `,variables:{ids:e.map((e=>e.id))}})).errors||this.grist.fetch()))}async _updateScenario(){var e=this.grist.dirtyRecords;if(e&&e.length){e=e.map((e=>{let t=e.id?{id:e.id}:{};const i=e.__dirtyfields__;for(let e in i)-1==["message","step","steps","progress","rounds"].indexOf(e)&&(t[e]=i[e].after);return t.cuFlag=e.__dirty__,t}));(await c.Lp.mutate({mutation:r.ZP`
          mutation ($patches: [ScenarioPatch!]!) {
            updateMultipleScenario(patches: $patches) {
              name
            }
          }
        `,variables:{patches:e}})).errors||this.grist.fetch()}}async startScenario(e){const t=(await c.Lp.mutate({mutation:r.ZP`
        mutation ($scenarioName: String!, $instanceName: String) {
          startScenario(scenarioName: $scenarioName, instanceName: $instanceName) {
            state
          }
        }
      `,variables:{scenarioName:e.name,instanceName:e.name}})).data.startScenario.state;e.state=t,(0,v.h4)({level:"info",message:`${z(t)?"success":"fail"} to start scenario : ${e.name}`}),this.grist.fetch()}async stopScenario(e){var t=await c.Lp.mutate({mutation:r.ZP`
        mutation ($instanceName: String!) {
          stopScenario(instanceName: $instanceName) {
            state
          }
        }
      `,variables:{instanceName:e.name}});t.errors?(0,v.h4)({level:"error",message:`${t.errors.map((e=>e.message)).join("\n")}`}):(0,v.h4)({level:"info",message:`success to stop scenario : ${e.name}`}),this.grist.fetch()}async startScenarioSchedule(e){const t=(await c.Lp.mutate({mutation:r.ZP`
        mutation ($scenarioId: String!) {
          startScenarioSchedule(scenarioId: $scenarioId) {
            scheduleId
          }
        }
      `,variables:{scenarioId:e.id}})).data.startScenarioSchedule.scheduleId;e.scheduleId=t,(0,v.h4)({level:"info",message:`${e.scheduleId?"success":"fail"} to start scenario schedule : ${e.name}`}),this.grist.fetch()}async stopScenarioSchedule(e){var t=await c.Lp.mutate({mutation:r.ZP`
        mutation ($scenarioId: String!) {
          stopScenarioSchedule(scenarioId: $scenarioId) {
            scheduleId
          }
        }
      `,variables:{scenarioId:e.id}});t.errors?(0,v.h4)({level:"error",message:`${t.errors.map((e=>e.message)).join("\n")}`}):(0,v.h4)({level:"info",message:`success to stop scenario schedule : ${e.name}`}),this.grist.fetch()}async exportHandler(){const e=this.grist.selected.length?this.grist.selected:this.grist.dirtyData.records,t=new Set(["id","name","type","description","schedule","timezone","steps"]);return e.map((e=>{let i={};for(const a of t)i[a]=e[a];return i}))}async importHandler(e){(0,v.Mw)(n.dy`
        <scenario-importer
          .scenarios="${e}"
          @imported="${()=>{history.back(),this.grist.fetch()}}"
        ></scenario-importer>
      `,{backdrop:!0,size:"large",title:l.z2.t("title.import scenario")})}};(0,a.gn)([(0,o.Cb)({type:Boolean}),(0,a.w6)("design:type",Boolean)],k.prototype,"active",void 0),(0,a.gn)([(0,o.Cb)({type:Object}),(0,a.w6)("design:type",Object)],k.prototype,"gristConfig",void 0),(0,a.gn)([(0,o.Cb)({type:Object}),(0,a.w6)("design:type",Object)],k.prototype,"taskTypes",void 0),(0,a.gn)([(0,o.IO)("ox-grist"),(0,a.w6)("design:type",s.N)],k.prototype,"grist",void 0),k=(0,a.gn)([(0,o.Mo)("scenario-page")],k)}}]);